Switch to full style
For C/C++ coders discussions and solutions
Post a reply

Reading file with integers

Sat Nov 08, 2008 10:50 pm

I'm trying to read a file filled with integers like this
967 834 1000 1669 2224 1978 -142 1462 -1036 205 -1355 1781 -673 461 -1009 1495
2442 -673 -64 -1109 1104 2402 -1347 -1208 -1118 -79 1216 2218 2395 -53 226 1271
2038 369 2412 167 799 -465 394 -797 2311 1822 833 173 -836 1641 2211 -1247 1368
47 2144 -838 -743 -1463 -641 -777 241 2029 -722 -1184 1535 690 342 -1212 606
-460

and put it into an array of integer.

My idea is to read the file with getLine() method (which will result a very long
string), split it with string tokenizer with space delimiter(I did not find a
method to do this automatically like StringTokenizer in Java) and covert it one
by one to integer and put it into an array.

I find this method is very exhaustive. Any best practice for this problem? Can I
read the file delimited with space instead of per line? Any tokenizer method
without doing it manually?



Re: Reading file with integers

Sat Nov 08, 2008 10:54 pm

Call fscanf() in a loop until you are done reading all the integers. Use %d
as the format string.

Post a reply
  Related Posts  to : Reading file with integers
 Reading the all file in php     -  
 Multithreaded File Reading     -  
 Reading and Writing To text file     -  
 Reading selected data from a source file     -  
 Reading a File Line by Line in php     -  
 Quicksort implementation C++ Code-Integers-Sorting     -  
 Encrypt/Decrypt a file from source file to target file.     -  
 Reading a Specific Character in php     -  
 Java Object Reading     -  
 Reading email in Python     -