Thread: Open file and print user selected number of lines

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Open file and print user selected number of lines

    I am getting compiler errors in my for loop and an illegal break within said for loop...Help?

    Errors:

    1>c:\users\robert\my programs\assignment 8_exercise 2\source.c(36) : error C2143: syntax error : missing ')' before ';'
    1>c:\users\robert\my programs\assignment 8_exercise 2\source.c(36) : error C2059: syntax error : ')'
    1>c:\users\robert\my programs\assignment 8_exercise 2\source.c(41) : error C2043: illegal break

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LINECOUNT 3;
    #define SOURCE_FILE "C:/Users/Robert/Desktop/TestFile1.txt"
    
    
    
    int main() 
    {
      char c;
      int lineCounter = 0;
      FILE *file; 
    
      file = fopen(SOURCE_FILE, "r"); 
    
      if(file==NULL) 
      {
        printf("Error: can't open file.\n");
        return 1;
      }
      else 
      {
        printf("File opened successfully. Contents:\n\n");
        
        for(lineCounter = 0; lineCounter <= LINECOUNT;;)
        {    
          c = fgetc(file);
          if(c = EOF) 
          {
             break;                            
          }
          else if(c = '\n')
          {
             printf("%c", c);
             lineCounter++;
          }
          else 
          {
             printf("%c", c);
          }
        }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        return 0;
      }
    }

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    > for(lineCounter = 0; lineCounter <= LINECOUNT;
    ;; wrong --> ;

    you aren't comparing, but assigning.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Still not getting it...

    So the code should read:
    Code:
    for(lineCounter = 0; lineCounter <= LINECOUNT;)
    ?

    Still doesn't work.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can break out of a loop, not out of an IF statement.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    And,
    Code:
    #define LINECOUNT 3;
    Don't use semi colons in #defines.
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Fixed the compiler errors, but no text prints...

    I was hoping this would print the first three lines of text?

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LINECOUNT 3
    #define SOURCE_FILE "C:/Users/Robert/Desktop/TestFile1.txt"
    
    
    
    int main()
    {
      char c;
      int lineCounter = 0;
      FILE *file;
    
      file = fopen(SOURCE_FILE, "r");
    
      if(file==NULL)
      {
        printf("Error: can't open file.\n");
        return 1;
      }
      else
      {
        printf("File opened successfully. Contents:\n\n");
       
        for(lineCounter = 0; lineCounter <= LINECOUNT;)
        {   
          c = fgetc(file);
          if(c = EOF)
          {
             break;                           
          }
          else if(c = '\n')
          {
             printf("%c", c);
             lineCounter++;
          }
          else
          {
             printf("%c", c);
          }
        }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        return 0;
      }
    }

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There's a difference between = and ==.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by steals10304 View Post
    I was hoping this would print the first three lines of text?

    Here is my code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LINECOUNT 3
    #define SOURCE_FILE "C:/Users/Robert/Desktop/TestFile1.txt"
    
    
    
    int main()
    {
      char c;
      int lineCounter = 0;
      FILE *file;
    
      file = fopen(SOURCE_FILE, "r");
    
      if(file==NULL)
      {
        printf("Error: can't open file.\n");
        return 1;
      }
      else
      {
        printf("File opened successfully. Contents:\n\n");
       
        /* it's a tad unusual to retrieve a whole file, char by char, but it can work */ 
        for(lineCounter = 0; lineCounter <= LINECOUNT;  ;)
        {
             
          c = fgetc(file);
          if(c == EOF)  /* take two on these */
          {
             break;                           
          }
          else if(c =='\n')
          {
             printf("%c", c);
             lineCounter++;
          }
          else
          {
             printf("%c", c);
          }
        }
    
        printf("\n\nNow closing file...\n");
        fclose(file);
        return 0;
      }
    }

    See the suggestions in blue, above. Also, the for loop will fail right off, because lineCount and lineCounter, are both zero, right?

    Rethink that one.

    If you want the user to select how many lines of the file are to be printed, then don't you need to get that input from the user?
    Last edited by Adak; 09-01-2009 at 11:27 AM.

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    hmm, that would be you aren't comparing but assigning. Guess I was too vague for a newbie.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Adak View Post
    See the suggestions in blue, above. Also, the for loop will fail right off, because lineCount and lineCounter, are both zero, right?
    LINECOUNT is a defined constant (3 here).
    Quote Originally Posted by Adak View Post
    If you want the user to select how many lines of the file are to be printed, then don't you need to get that input from the user?
    Surely the user will know to change the constant in the source and recompile? [ducks and runs far away]

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    34

    Thank you, it works, I am trying to add one more feature...

    I would like the program to wait for user to hit the newline (Enter key) after it prints the first 3 lines of text, if the enter key is hit the program will print three more lines of text, this process will continue for length of document. If any other key is hit before the newline, the program will terminate. Any ideas? Here is my code so far:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #define LINECOUNT 3
    #define SOURCE_FILE "C:/Users/Robert/Desktop/TestFile1.txt"
    
    
    
    int main()
    {
      char c;
      int lineCounter = 0;
      FILE *file;
    
      file = fopen(SOURCE_FILE, "r");
    
      if(file==NULL)
      {
        printf("Error: can't open file.\n");
        return 1;
      }
      else
      {
        printf("File opened successfully. Contents:\n\n");
       
        for(lineCounter = 0; lineCounter < LINECOUNT; )
        {   
          c = fgetc(file);
          if(c == EOF)
          {
             break;                           
          }
          else if(c == '\n')
          {
             printf("%c", c);
             lineCounter++;
          }
          else
          {
             printf("%c", c);
          }
        }
        
        printf("\n\nNow closing file...\n");
        fclose(file);
    
    
        
        return 0;
      }
    }

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Do you know how to get input from the keyboard? Do you want to get input without waiting for the enter key? (That's an FAQ here, if you search on it.)

  13. #13
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Quote Originally Posted by tabstop View Post
    LINECOUNT is a defined constant (3 here).


    Surely the user will know to change the constant in the source and recompile? [ducks and runs far away]
    tabstop... and the Hat of Running.
    Mainframe assembler programmer by trade. C coder when I can.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    34
    Quote Originally Posted by tabstop View Post
    Do you know how to get input from the keyboard? Do you want to get input without waiting for the enter key? (That's an FAQ here, if you search on it.)
    I know how to get input from the keyboard. I do not know how to loop the program so that it prints three lines, waits for the enter key to print three more ect...

  15. #15
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by steals10304 View Post
    I do not know how to loop the program so that it prints three lines, waits for the enter key to print three more ect...
    Build a loop, and use the input to determine whether the loop continues.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  2. Scope And Parameter Passing
    By djwicks in forum C Programming
    Replies: 6
    Last Post: 03-28-2005, 08:26 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM