Thread: printing out a line from text file..

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    44

    printing out a line from text file..

    i want to open a txt file and then store each line in the file into an array of structs.. this iswhat i have so far

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct input{
    char *sentence[300];  
    };
    
    int num_lines(char name[]) // this function counts the number of lines in file
    {
        int ch, prev = '\n', lines = 0;   
        FILE           *in_file;
        in_file = fopen(name, "r");
        if (in_file == NULL) {
            fprintf(stderr, "Unable to locate the file!\n");
            exit(8);
        }
        while ( (ch = fgetc(in_file)) != EOF ) { // read chars in file
            if ( ch == '\n' )
                            {
                                    ++lines; // counter
                            }
                            prev = ch; 
                    }
                    fclose(in_file);
                            if ( prev != '\n' ) // if last line did not end in new line
                    {
                            ++lines;
                    }        
        return lines;
    
    }
    
    int main(int argc, char *argv[])
    {
    
        input line[500];
        int numline,x;
        numline = num_lines(argv[1]);
        FILE           *in_file;
        in_file = fopen(argv[1], "r");
        if (in_file == NULL) {
            fprintf(stderr, "Unable to locate the file!\n");
            exit(8);
        }
        for(x=0;x<numline;x++)
        {
          fscanf(in_file,"%300[^\n]s",line[x].sentence);
          printf("Line %d:%s\n",x,line[x].sentence);
         }                      
        
        fclose(in_file);
        
      
        getch();
        return 0;
    }
    when i run it it seems to only store in array 0.. whats wrong? if any1 knows the solution can you plz explain it in detail as im a beginner ty appreciate it.
    Last edited by loso44x; 10-22-2005 at 03:08 AM.

  2. #2
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    A few issues.

    For starters, you have:
    Code:
    char *sentence[300];
    In your struct, which means declare an array of 300 pointers, you want char sentence[300], an array of 300 chars, I presume.

    Also, you cannot do
    Code:
     input line[500];
    in C, only C++. Make a typedef if you want to do that, or declare it as struct input line[500];

    Also, your fscanf format is wrong, the format specifier is %300[^\n], you don't need the s on the end. Because of the s, fscanf is looking for an s after the \n. The [foo] specifer is a format specifier on its own, it doesn't need an s.

    Edit: Also, you need some whitespace after the format string: "%300[^\n]\n", so fscanf can read the whitespace off the stream. It can be any whitespace character.

    Finally, it should be %299, not %300, because you have a 300 element char array and need room for the null terminator.
    Last edited by cwr; 10-22-2005 at 03:22 AM.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    44
    hi thanks for the help but there seems to be a problem when i put a double space in the text file it doesnt store it in the array .. but rather the last array
    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    struct input{
    char sentence[300];  
    };
    
    int num_lines(char name[]) // this function counts the number of lines in file
    {
        int ch, prev = '\n', lines = 0;   
        FILE           *in_file;
        in_file = fopen(name, "r");
        if (in_file == NULL) {
            fprintf(stderr, "Unable to locate the file!\n");
            exit(8);
        }
        while ( (ch = fgetc(in_file)) != EOF ) { // read chars in file
            if ( ch == '\n' )
                            {
                                    ++lines; // counter
                            }
                            prev = ch; 
                    }
                    fclose(in_file);
                            if ( prev != '\n' ) // if last line did not end in new line
                    {
                            ++lines;
                    }        
        return lines;
    
    }
    
    int main(int argc, char *argv[])
    {
    
        struct input line[500];
        int numline,x;
        numline = num_lines(argv[1]);
        FILE           *in_file;
        in_file = fopen(argv[1], "r");
        if (in_file == NULL) {
            fprintf(stderr, "Unable to locate the file!\n");
            exit(8);
        }
        for(x=0;x<numline;x++)
        {
          fscanf(in_file,"%299[^\n]\n",line[x].sentence);
          printf("Line %d:%s\n",x,line[x].sentence);
         }                      
        
        fclose(in_file);
        
      
        getch();
        return 0;
    }
    example.txt
    Code:
    this is an example text file
    what
    huh
    lol
    
    2
    output
    Code:
    Line 0:this is an example text file
    Line 1:what
    Line 2:huh
    Line 3:lol
    Line 4:2
    Line 5:

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    I think it's because of this line:
    Code:
    fscanf(in_file,"%299[^\n]\n",line[x].sentence);
    Try
    Code:
    fgets(line[x].sentence, sizeof(line[x].sentence), in_file);
    and remove the newline off of line[x].sentence, or print it without a newline.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading text file by line number
    By whiskedaway in forum C++ Programming
    Replies: 13
    Last Post: 06-16-2009, 10:09 AM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM