473,326 Members | 2,133 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

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

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
8 7226
gpraghuram
1,275 Expert 1GB
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
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
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
1,275 Expert 1GB
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
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
1,275 Expert 1GB
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
Thanks dear,
thanks for all ur msgs..
have a good time..
Apr 20 '07 #8
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

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

Similar topics

5
by: Mika M | last post by:
Hi! I'm trying to read text file like... "Field1";"Field2";"Field3";"Field4" "ABCD";"EFGH";"1234";"20051020" "AABB";"CCDD";"2468";"20051021" "CCDD";"XXYY";"4321";"20051022" ....using OLE...
5
by: leesa | last post by:
I have a number field in a sequential file. Im going to read this file, sum up this number and display the grand total in my application using VB. Do anyone know how to do that?? Thanks in...
3
by: xuxf055 | last post by:
Hi Everybody, I am confused for a few days, how to read the data file seperated by comma. for exampe following file. data.txt 1,2,3,4,5,6,7,8,9,10 11,12,13,14,15,16,17,18,19,20 how to read it...
8
by: Johnny | last post by:
Hi all: I have an ASP.NET form that reads an Excel file and populates a datagrid. If I load the form in IE on the server, and select a local file, the code works fine. However if I load the form...
3
by: Rupali12345 | last post by:
hi all I m trying to read an .dcm file using byte array.But i dont know actually how to read byte code from file.please give me any example of reading byte code. I search in google also...but in...
1
by: neveen | last post by:
i want to open and read text file using j2me that can run on mobile 6630 then i want to make button called read that when pressed the data inside text display
1
by: =?Utf-8?B?c3Vpcw==?= | last post by:
Hi every body, i have a problem with how to read .MSG file (Outlook) using vb.net ? using this articles the author has already explain how to read .MSG file using stuctured storage (vc++) ...
3
by: sasiee2k | last post by:
Dear All, I have a text file. I want use the c#.net to read the file and place an 'A' at the end of every line if the line was read. And place the data in the sql server database. regards, sasi
7
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am using visual studio C# window and I have an xml file which I need to read that file using Object Oriented program. What codes I should use in my class to load and read that xml file....
4
by: tokcy | last post by:
Hi everyone, I have some .rtf extension file and i want to read that file using php on html page but i am not able to do this. If it is .txt file then there is no problem but it is RTF and DOC...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.