This is straight C, not C++ or MFC. I want to know and how I can use fscanf and fputs to read a data file without determining the length. Here is what I am talking about.
My data file is in the form of
8.5
3.14
9.5
0.2
1.1
Now, it will be easier to do if I allow user to enter the length of the data, but I don't want that. I simply want user to enter the file name.
I would like to put fscanf inside a while loop and increment a counter for each line. After the end of the last line, I want to use malloc to allocate space for an array and put the value to that array. I want to know if it is possible and how I can do it.
You have many options, like:
1. Precount rows in file, rewind the file and then allocate exact number of rows
2. Using a linked list
3. Allocate a fix number of array entries (with malloc) and let it grow if needed
I understand number 3 and I probably go with it, but I like the idea of precount the number of row in the file. Do you know how can I do that inside a while loop? Is there any way you can show me how to do it with a snap code?
Thank you, this is the first time I see and heard the rewind function, I have never use it. By looking at its description from MSDN, it makes a lot of sense.
Base on what you said, I don't need while loop. The for loop should do it.
Bookmarks