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

Read data from File in columnwise Emergency

P: 3
hi guys, I hv problem in reading data from text file. I want to read data in column wise from the table shown as below

Module Ad Di Do
4011 1 2 -
4012 2 3 2
4013 2 3 3
4014 4 7 8

i m looking for a program which is going to read MODULE column and store it in a Char *Buf[ ] for eg.

Buf[0]=Module
Buf[1]=4011
Buf[2]=4012
Buf[3]=4013.............like this
i also try some code but i didn't get desired o/p
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. # define LINE_LENGTH 80
  5.  
  6.  
  7.  main()
  8.  {
  9.  
  10.        FILE *fp;
  11.  
  12.        char *Buf[51];
  13.  
  14.        char line[LINE_LENGTH ];
  15.  
  16.  
  17.        int i,j,k=0;
  18.  
  19.        int pos;
  20.  
  21.  
  22.        fp = fopen("/root/pankaj/c1.txt","r");
  23.  
  24.  
  25.        if(fp<=0)
  26.         {
  27.  
  28.             printf("Error! File couldn't open");
  29.  
  30.         }
  31.  
  32.         while(fgets(line,LINE_LENGTH,fp)!=NULL)
  33.        // while(sscanf(line,"%s",n)!=EOF)
  34.           {
  35.               pos= ftell(fp);
  36.               Buf[i]=line;              
  37.               printf("\n%d",pos);
  38.  
  39.              i++;
  40.           }
  41.  
  42.        for(k=0;k<6;k++)
  43.           {
  44.  
  45.               printf("%s",Buf[k]);
  46.  
  47.            }
  48.  
  49.  }                                                                                                
  50.  
what i got always is
Buf[0]=4014
Buf[1]=4014
Buf[2]=4014
Buf[3]=4014
End value of file i get confused what actually happening
So Plz suggest me what r the changes to be done
I m new in C programming i want this o/p immediately
May 9 '07 #1
Share this Question
Share on Google+
2 Replies

Expert 10K+
P: 11,448
I moved your question from the Articles section to the Forum section. Please
don't post questions in the Articles section; thanks.

kind regards,

Jos
May 9 '07 #2

AdrianH
Expert 100+
P: 1,251
Well, there is a minor error and one very big one.
Expand|Select|Wrap|Line Numbers
  1. #include<stdio.h>
  2. #include<string.h>
  3.  
  4. # define LINE_LENGTH 80
  5.  
  6.  
  7.  main()
  8.  {
  9.  
  10.        FILE *fp;
  11.  
  12.        char *Buf[51];
  13.  
  14.        char line[LINE_LENGTH ];
  15.  
  16.  
  17.        int i,j,k=0;
  18.  
  19.        int pos;
  20.  
  21.  
  22.        fp = fopen("/root/pankaj/c1.txt","r");
  23.  
  24.  
  25.        if(fp<=0)
  26.  
fp is actually a pointer and you should be saying if (fp != NULL). However, since there is no pointer less than 0, what you did is legal, if not a bit unusual.
Expand|Select|Wrap|Line Numbers
  1.         {
  2.  
  3.             printf("Error! File couldn't open");
  4.  
  5.         }
  6.  
  7.         while(fgets(line,LINE_LENGTH,fp)!=NULL)
  8.        // while(sscanf(line,"%s",n)!=EOF)
  9.           {
  10.               pos= ftell(fp);
  11.               Buf[i]=line;              
  12.               printf("\n%d",pos);
  13.  
  14.              i++;
  15.           }
  16.  
It is here in that loop that is giving you the problem. It is a bit scrambled due to your attempt to debug, but it is clear to see what the problem is.

Buf is an array of char pointers. You are assigning each element to the line char array. Thus at the end, you apparently get the last value read. There are a couple of ways to fix this. The easiest way is not to use an array of char pointers. Use an array of ints instead. That would mean using a format string of %d instead of %s.

The other way would require you to make Buf an array of array of chars. Then use scanf to dump directly into each element it instead of line (or sscanf into each element from line).

A third way would be to dynamically allocate each Buf element as needed, and then copy into each newly allocated pointed at element.
May 9 '07 #3

Post your reply

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