I have a data set in .txt format and want to do some arithmetic operation on the data set using C code. How to do the job? I want to make the text data into a 2D-array. The data set is like this:
1st col - year, 2nd col-January rainfall, 3rd col-Feb rainfall, 4th col-March rainfall

Year J F M

1976 9.7 10.2 4.9
1977 15 3.4 3

Edited by srg85: small change

6 Years
Discussion Span
Last Post by mridul.ahuja

How much do you already know how to do? Do you know how to open a file and read it one word at a time? (A word is all the characters between spaces). Have you tried to code that program yet? If not, you should.


You need to declare your array first
Open the file and
Read the content of the file into the array. Where do you want to start from


Well, 1st of all, you should check the problem info/statement and analyse it a bit.
You have to have in the end a 2x2 array, meaning with everything that is in the file, like a maxtrix.
I don't know on what os are you trying to code. If you do code on UNIX than go to terminal and write man (from manual) fopen, or man popen, or just system call open() and see what happens.
After that, you should check the functions of the FILE *fd items - read/write.
A quick search on google wouldn't had hurt you would it?
Start small, with small scripts, and than after you feel stable on your knowledge try doing the actual homework.
Hope my advices helped you.
If you have further questions let us know, but first I want to see you tried.

Votes + Comments
thanx for ur reply.i m using XP os..i tried bt could nt read text file into array,bcz the first row of the data file contain months and the first col contains years.but in the array these sholud not come.when starting reading file, it starts with the firs

   char ch, file_name[25];
   FILE *fp;

   printf("Enter the name of file you wish to see ");

   fp = fopen(file_name,"r");      // opening file in read mode

   if( fp == NULL )
      perror("Error while opening the file.\n");

   printf("The contents of %s are :- \n\n", file_name);

   while( ( ch = fgetc(fp) ) != EOF )        //   reading file..

   return 0;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.