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

Recommended Answers

All 4 Replies

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?
http://www.phanderson.com/files/file_read.html
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.

commented: 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 +0
#include<stdio.h>
#include<stdlib.h>

main()
{
   char ch, file_name[25];
   FILE *fp;

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

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

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

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

   while( ( ch = fgetc(fp) ) != EOF )        //   reading file..
      printf("%c",ch);

   fclose(fp);
   return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.