Thread: adding line numbers and concatenating a filename

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Since you are the end of the file, you need to seek to the beginning of the file again.
    (Just as you finished reading a book, you need to travel to the beginning of the book again if you want to continue reading.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  2. #17
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    So, yeah, try fseek. Or just fclose() and fopen() the file again (fseek is more correct).

    Also fix your codeing style. It should be like this:
    Code:
         else 
         {
                /* read one character at a time from file, stopping at EOF, which
                   indicates the end of the file. */
                {
                      for (ch = getc(file);  ch != EOF;  ch = getc(file))
                      putc(ch, newfile);
                }
                char line[LINE_LEN], linein[NAME_LEN],lineout[NAME_LEN];
                for (status = fgets(line, LINE_LEN, file);
                      status != 0;
                      status  = fgets(line, LINE_LEN, file))
                {
                      if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
                      fprintf(newfile, "%3>> %s\n", ++i, line);
                } 
          }
    You MUST have correct the spaces and the {,} or the code is difficult to read. The above code is simple, but why not make it more readable?

    If it is a copy/paste problem, my apologies

  3. #18
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    Thanks for the help and tips. So when using fseek() do I need to tell the program anything additional, or will this work (and should I look for the beginning of the original file or 'newfile'?
    Code:
    fseek (file, 0L, SEEK_SET);
    char line[LINE_LEN], linein[NAME_LEN],lineout[NAME_LEN];
                for (status = fgets(line, LINE_LEN, file);
                      status != 0;
                      status  = fgets(line, LINE_LEN, file))
                {
                      if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
                      fprintf(newfile, "%3>> %s\n", ++i, line);
                } 
          }

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, that simply fseek is correct. But consider replacing that for with while:
    Code:
                while ( (status = fgets(line, LINE_LEN, file)) != NULL )
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #20
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    Thanks Elysia...and for the record I did change that 0 to NULL when you suggested it earlier...The code compiles fine but when I execute I get a windows error? Any ideas? I'm going to try on another pc, just curious...

  6. #21
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What error and where?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #22
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    The popup "windows encountered an error and must close the program..." It happens after I compile and run the code...

  8. #23
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    Happens on the laptop too!!! Hmmm......

  9. #24
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    fprintf(newfile, "%3>> %s\n", ++i, line);
    The format is incorrect. What are you trying to write?
    %3 is not a format type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #25
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    I'm trying to insert line numbers for ever line copied from the original document into every line in the new document, with the exception of the heading...

  11. #26
    Registered User
    Join Date
    Jun 2008
    Posts
    15
    Okay- so the error went away and I get the line numbers to print. Must have been bad code in
    there somewhere, but don't ask me where. Unfortunately the output is duplicated instead of
    replaced...Hmmm...
    Code:
    {
               /* read one character at a time from file, stopping at EOF, which
               indicates the end of the file. */
               {
                         for (ch = getc(file);ch != EOF; ch = getc(file))
                         putc(ch, newfile);
               }
            
            fseek (file, 0L, SEEK_SET); //return to the beginning of the file
            char line[LINE_LEN], namein[NAME_LEN],nameout[NAME_LEN];
            while ( (status = fgets(line, LINE_LEN, file)) != NULL )
            {
                if (line[strlen(line) -1] == '\n') line[strlen(line) - 1] = '\0';
                fprintf(newfile, "%d\t%s\n", ++i, line);
            }
    
            }
    The output looks like this:

    ***end of first pass through, I think this is the putc()?***
    Code:
    If oil prices stay high as expected, the revenues would create a substantial surplus for Iraq at a 
    time when Americans are facing an economic slump, aggravated by painfully high gas prices.
    
    Click to Print Find this article at: 
    Copyright ? 2008 Time Inc. All rights reserved.
    Then it moves on to the second pass through... fseek()?
    Code:
    1	Military Dealing With High Gas Prices
    2	By AP/ANNE FLAHERTY
    3	
    4	(WASHINGTON) — Consumers at the gas pump aren't the only ones suffering sticker
    5	 shock. Military units...
    Last edited by durrty; 06-28-2008 at 03:44 AM. Reason: Hope it's easier to read...

Popular pages Recent additions subscribe to a feed