0
#include <stdio.h>

void stripnl(char *str) {
  while(strlen(str) && ( (str[strlen(str) - 1] == 13) || 
       ( str[strlen(str) - 1] == 10 ))) {
    str[strlen(str) - 1] = 0;
  }
}

int main() {
  FILE *infile;
  char fname[40];
  char line[100];
  int lcount=1,i=0;
  char mean[100];

  /* Read in the filename */
  printf("Enter the name of a ascii file: ");//input txt or cel file
  fgets(fname, sizeof(fname), stdin);

  /* We need to get rid of the newline char. */
  stripnl(fname);
 /* Open the file.  If NULL is returned there was an error */

      if((infile = fopen(fname, "r")) == NULL)
       {
            printf("Error Opening File.\n");
            exit(1);
      }
  
  
      for(lcount=0;lcount<25;lcount++)
      {
          while( fgets(line, sizeof(line), infile) != NULL)
           {
                 
                printf("%s", line);  
          }
      }
      fclose(infile);  /* Close the file */
}

can anybody help me out how to extract a column from txt or cel file format.....thnxx in advance....

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

fgets() appends the '\n' to the end of the string, so you have to truncate it before you can use fname to open the file

fgets(fname ...);
if( fname[strlen(fname)-1] == '\n')
    fname[strlen(fname)-1] = 0;

line 36: The answer to your question can be done a couple ways. I would prefer strtok() to extract the words from the line read.

char* word;
word = strtok(line," "); // assume columns are separated by space
while( word != NULL)
{
   // do something with this word
   //
   //
   word = strtok(NULL, " "); // get next word
}

Another way is to use a pointer and iterate through the string.

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.