#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Post Read line from file, edit, and write to new file - help please


    Hi, so I want my code to remove all leading white space from an array (read in from a text file) and put this edited version of the char array into a new array and print to a file. I believe I have most of the syntax correct, but the bot where I am making the new array seems to mess-up somehow. it just puts a line of junk text in my file, like one letter from every line or so. code is below, any help would be appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char* argv[])
    {
        FILE *input_file = fopen(argv[1],"r");
        FILE *output_file = fopen("output.c","w");
        char line[150];
        char fmtline[150];
        char *start;
        int i;
        start = &fmtline[0];
    
        do
        {
        int a, b = 0;
        start = &fmtline[0];
    
            if (input_file == NULL)
            {
                    printf("Cannot open file");
                    exit(EXIT_FAILURE);
            }
    
            while (line[a] != '\0') //copy routine loop
            {
                    if (line[a] == ' ')
                    {
                    a++;
                    }
                    else
                    {
                            for(i=0;i<(sizeof(fmtline));i++);
                            {
                                    fmtline[b] = line[a];
                                    a++;
                                    b++;
                            }
                    break;
                    }
            }
    
            fputs(fmtline,output_file);
            //fputs(line,output_file);
            fgets(line, 150, input_file);
    
        }   while(feof(input_file) == 0);
    
        fclose(input_file);
        fclose(output_file);
    }
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,704
    Rep Power
    480
    The problem is likely the ; empty statement of the for loop:

    for(i=0;i<(sizeof(fmtline));i++);

    other thoughts: didn't test that output file opened;
    if you did remove semi-colon as suggested and there is white space at the start of a line then the loop overruns the buffer; main program returns a chosen status only when the input file won't open.

    Comments on this post

    • 06s23 agrees
    Last edited by b49P23TIvg; December 10th, 2013 at 02:44 PM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    The problem is likely the ; empty statement of the for loop:

    for(i=0;i<(sizeof(fmtline));i++);
    What do you mean, the sizeof() ? Becuase I have tried changing it to 150 and the same thing still happens :/
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Thumbs up


    Originally Posted by b49P23TIvg
    The problem is likely the ; empty statement of the for loop:

    for(i=0;i<(sizeof(fmtline));i++);
    Ah i see what you mean, I removed the ";" from the end.
    It sort of worked. It now prints the first line properly to the file, but now i have junk characters and a load of ^^^^^^ ect

    Thanks though, this will be eaisier to fix :)
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,704
    Rep Power
    480
    other thoughts: didn't test that output file opened;
    if you did remove semi-colon as suggested and there is white space at the start of a line then the loop overruns the buffer; main program returns a chosen status only when the input file won't open.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    C:TEST>gcc -Wall six01.c
    six01.c: In function `main':
    six01.c:52: warning: control reaches end of non-void function

    C:TEST>
    You said you were going to return an int. Please keep your promises.

    Code:
        do
        {
        int a, b = 0;
        start = &fmtline[0];
    
            if (input_file == NULL)
            {
                    printf("Cannot open file");
                    exit(EXIT_FAILURE);
            }
    Good that you test for having successfully opened the input file. But why are you doing it for every single line that you are inputting? You only need to do this test once, before you enter the do-while loop.

    Code:
            while (line[a] != '\0') //copy routine loop
            {
                    if (line[a] == ' ')
                    {
                    a++;
                    }
                    else
                    {
                            for(i=0;i<(sizeof(fmtline));i++);
                            {
                                    fmtline[b] = line[a];
                                    a++;
                                    b++;
                            }
                    break;
                    }
            }
    
            fputs(fmtline,output_file);
            //fputs(line,output_file);
            fgets(line, 150, input_file);
    
        }   while(feof(input_file) == 0);
    That is the only place that you read in a line of input. So the very first time you read in a line of input is after you have processed the "first line of input". That means that that "first line" that you processed was nothing but random garbage. Hence the first line of garbage in your output file.

    Before you process a line of input, you need to have read in a line of input.

    You could do an fgets before entering the do-while loop (and after the test for the input file being open).

    Or you could place the fgets at the top of the do-while loop followed by an if to test for EOF; if the read was successful, then process it.

    Or use a regular while loop preceded by an fgets; if the very first read fails, then there will be nothing to process, right?

    Think about what needs to be done and you should see how to do it.
    Last edited by dwise1_aol; December 10th, 2013 at 02:56 PM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0

    Thumbs up


    ok, many thanks people, I normally make stupid mistakes (like the test in the while loop) when im first writing code, iv not been doing it for very long, but obviously,all this advice is extremely helpful.
    Thanks again , and I will post the solution when I tamper around abit. :)
  14. #8
  15. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,704
    Rep Power
    480
    The solution is

    sed 's/^ *\(.*\)/\1/' <input >output
    [code]Code tags[/code] are essential for python code and Makefiles!
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by b49P23TIvg
    The solution is

    sed 's/^ *\(.*\)/\1/' <input >output
    I can implement sed scripts in C?
    Obviously, I took one look at this work and instantly thought I could make a shell script to deal with this.
    But the reality of college assignments leadds me to think I have to use C only.... :(
  18. #10
  19. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,090
    Rep Power
    2222
    You need to be careful, because sometimes b49P23TIvg takes "there's more than one way" (Larry Wall, creator of Perl) a bit too far.

    When you are in a C class, then of course you have to use C. But when you write a shell script, where do you think all those utilities you call came from? And one of the things you can do with C would be to write your own utilities and filters -- when I was first teaching myself C, my self-assigned learning projects were command-line utilities for MS-DOS, like a better DIR (MS-DOS version of ls).
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2013
    Posts
    16
    Rep Power
    0
    Originally Posted by dwise1_aol
    You need to be careful, because sometimes b49P23TIvg takes "there's more than one way" (Larry Wall, creator of Perl) a bit too far.

    When you are in a C class, then of course you have to use C. But when you write a shell script, where do you think all those utilities you call came from? And one of the things you can do with C would be to write your own utilities and filters -- when I was first teaching myself C, my self-assigned learning projects were command-line utilities for MS-DOS, like a better DIR (MS-DOS version of ls).
    I thought as much. thanks for the clarification :P
    And I am definitely going to get some self-motivated projects done when I can. Practice makes perfect after all.

IMN logo majestic logo threadwatch logo seochat tools logo