You are here:

C++/Read file

Advertisement


Question
Dear Sir,
Thank you very much for your kindly reply, i still have some doubts here...after reading 10 numbers from the num.txt, i try to list out all the 10 numbers, then sum them up, and finally print out the total of sum, but nothing display in the end, not even the numbers from the file, but no error stated......please help, Thanks...

#include <iostream.h>
#include <stdio.h>
#include <Windows.h>
void main(){
FILE *pFile = NULL;
int bitfield[10], count = 0, sum=0;
pFile = fopen("num.txt", "r");

if(!pFile)
MessageBox(NULL, "Could not open the file", "Error", MB_OK);

while(!(feof(pFile))) {
fscanf(pFile,"%d", &bitfield[count++]);
cout << bitfield[count++] << "\n";
sum=sum + bitfield[count++];
}
cout << "Total of 10 Numbers is: " << sum;
}


-------------------------
Followup To
Question -
Dear Sir,
I have tried the following but 3 errors occur, please explain how to resolve the errorsĄ­.ThanksĄ­

#include <iostream.h>
#include <stdio.h>

void main(){
FILE *pFile = NULL;
int bitfield[10], count = 0;
pFile = fopen("num.txt", "r");

if(!pFile)
MessageBox(NULL, "Could not open the file", "Error", MB_OK);

while(!(feof(pFile)))
fscanf("%d", &bitfield[count++]);
}

Errors:
error C2065: 'MessageBox' : undeclared identifier
error C2065: 'MB_OK' : undeclared identifier
error C2664: 'fscanf' : cannot convert parameter 1 from 'char [3]' to 'struct _iobuf *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast


-------------------------
Followup To
Question -
Dear sir,

can you tell me how to Read a number field in a sequential file so that i can sum up this number in C++? Thanks..


Answer -
Hello,

Sorry about that earlier. I was in a hurry when I answered. To get rid of the first and second error, #include <Windows.h>. It contains the MessageBox and MB_OK definitions. For the third error, use this instead:

while(!(feof(pFile)))
fscanf(pFile, "%d", &bitfield[count++]);

The first parameter to fscanf is the pointer to the file stream. Again, sorry about that.

I hope this information was helpful.

- Eddie

Answer
Hello,

The reason that your above code isn't working is because you are incrementing your counting variable more than once in the while loop. This means that it is skipping array subs. The ++ postfix operator increments the number after the number is used, which is why I did it like that. ie.

int count = 0;
bitfield[count++} //.....

starts out at bitfield[0], then it increments itself after that line of code has been executed. To fix the code above simply substitute this new code:

while(!(feof(pFile)))
{
fscanf(pFile,"%d", &bitfield[count]);
cout << bitfield[count] << "\n";
sum=sum + bitfield[count++];
}
cout << "Total of 10 Numbers is: " << sum;
}

That way, it only increments the count variable after the last line has been executed, and thus would go to 1, 2 ,3... after each time the loop runs.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.