By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,028 Members | 1,784 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,028 IT Pros & Developers. It's quick & easy.

HOW TO READ DATA FROM A FILE USING C or C++ PROGRAMMING?

P: 10
Dear friend,
i am new to this forum and i hope my problem will be solved here...
Here is my query..
I have a text file which has some 10000 values in following order..

0.004626 0
-2.155648 0
-0.714710 0
0.705074 1
0.708944 1
.......
I want to copy all these values in an array. the array should hold values like this:
array[0]=0.004626
array[1]=0
array[2]=-2.155648
array[3]=0 and so on.......

I can use C or C++. i am a biggner in programming.
waiting for your replies...
Apr 20 '07 #1
Share this Question
Share on Google+
8 Replies


gpraghuram
Expert 100+
P: 1,275
Hi,
In C you have to follow these steps
1)Open the file using fopen
2)Declare a array which can hold 1000 objects..or better declare a pointer and allocate at run time
2)in a while loop check for EOF
2.1)Have a count and increment it after evry read
2.2)Read the variable using fscanf and pass the variable as input to it
3)Close the file

Try these steps to write ur code and if u face any issues, post ur issue so we can help you to solve it.

Thanks
Raghuram
Apr 20 '07 #2

P: 10
dude thanks for ur reply ...
frankly speaking, i dont know any file concepts in C or C++..

i request you to write a simple code for me which can solve my query..
waiting for reply..
Apr 20 '07 #3

P: 10
dear i tried this code.. have a look..
main()
{
FILE *kp;
char classmain[10000];
int i=0, j=0,x=0;
kp=fopen("s11.pat","rb");
fread(classmain,0,10000,kp);
if(kp==NULL)
{
printf("unable to open the file");
exit();
}
while(fgets(classmain,36,kp) != NULL)
fputs(classmain, stdout);
}

The above code is displayng the full file content but its not storing it in an arrray.. so what changes r required in the code..
Apr 20 '07 #4

gpraghuram
Expert 100+
P: 1,275
Hi,
You have to strore the value after reading it.
You have to declare a double array for storing values.
Expand|Select|Wrap|Line Numbers
  1. while(!feof(kp))
  2. {
  3.      fscanf(kp,"%d ",&(classmain[i]));
  4.      i++;
  5.     fscanf(kp,"%d ",&(classmain[i]));
  6.     i++
  7. }
  8.  
For more info refer to this thread

Thanks
Raghuram
Apr 20 '07 #5

P: 10
Thanks a lot for your replies.. i think their is some problme with my C complier.
can u tell me from where to download it.. i just want to start evevrything from the beginning.. i got fed up with this program..
PLZ send me a link from where i can download C.

Waiting for reply..

thanks a in advance.
Apr 20 '07 #6

gpraghuram
Expert 100+
P: 1,275
Hi,
I dont know from where u can download c compiler.
I think you can download gcc from GNU wesite...
Better search it and find it.

Thanks
Raghuram
Apr 20 '07 #7

P: 10
Thanks dear,
thanks for all ur msgs..
have a good time..
Apr 20 '07 #8

P: 6
I have the same question but I want it to read text file by c++ code

plz, I want simple code just to read
Apr 20 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.