Storing using fgets issue

This is a discussion on Storing using fgets issue within the C Programming forums, part of the General Programming Boards category; Hi, I'm writing a basic line editor program, I'm trying to get my insert function working, but for some reason ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    3

    Storing using fgets issue

    Hi,

    I'm writing a basic line editor program, I'm trying to get my insert function working, but for some reason when storing my lines it only correctly does so for the first line of input and then for proceeding lines is missing the first character in that line.

    For eg:
    OUTPUT:
    ? i
    Hello
    World
    .
    ? p
    Hello
    orld
    ?


    SHOULD BE:
    ? i
    Hello
    World
    .
    ? p
    Hello
    World
    ?


    I believe it is an issue with having the fgetc within the following loop because testing my program with the single fgets it works fine, but I need someway to account for a "." in stdin to break the insertion of text.

    If someone could help me, that would be greatly appreciated.



    Code:
       while (1) {
           char c = getCmd();    
           if (c == 'x') {
             printf("Forced exit, changes not saved\n");
             break;
           } else if (c == 'h') {
             //call printhelpList
             printhelpList();
           } else if (c == 'i') {
             while (fgets(line, LINELENGTH, stdin)) {
                lines[numLines] = strdup(line);
                numLines++;          
                if (fgetc(stdin) == '.') {
                    if (fgetc(stdin) == '\n') {
                        break;
                    }
                }
                
             }
           } else if (c == 'p') {
            //call printLines
            printLines(lines, numLines); 
           
           }

  2. #2
    cas
    cas is offline
    Registered User
    Join Date
    Sep 2007
    Posts
    991
    Yes, the problem is that you're unconditionally reading the first character from the next line. You could use ungetc() to push it back if it's not a '.'; but why not just check the whole line against ".\n" after your fgets()? If it matches, break and don't store the line.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    3
    Hmm I tried something like this:
    Code:
      if (strcmp(line, ".\n") == 0) {
                    break;
                }
    It manages to break, but how would I avoid storing that line (i.e. the final line with the ".") ?


    Regards

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    3
    Oh sweet, you were right, I just had the order the wrong way, so I immediately should've implemented the check for the ".\n" before anything else.


    Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 07:15 PM
  2. bandwidth issue / network issue with wireless device communication
    By vlrk in forum Networking/Device Communication
    Replies: 0
    Last Post: 07-05-2010, 11:52 PM
  3. fgets issue
    By Fox101 in forum C Programming
    Replies: 2
    Last Post: 05-05-2008, 10:11 PM
  4. fgets() and structures issue
    By gettyUP in forum C Programming
    Replies: 3
    Last Post: 12-21-2006, 10:26 AM
  5. fgets & storing
    By hyaline in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 11:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21