ow can i use fgets to read and ignore the first two lines of a file and output into another file

Discussion in 'C Programming' started by Justme, Sep 30, 2006.

  1. Justme

    Justme Guest

    Novice programmer needs help with using fgets to read and ignore the
    first two lines of a file. I've gone thru the previous posting
    regarding fgets, but none of them seems to help my situation.
    I have airdata file that i have to read, but in other teh fscanf to
    work properly, i need to ignore the first two lines, because scanf does
    not read spaces.

    This is what my current code looks like

    #include <stdio.h>
    #include <sting.h>
    #include <stdlib.h>

    int main()
    {
    FILE * AirFile; //the file that contains the data to be read
    FILE *textFile; //the out file for both lines


    char text1[1000];
    char text2[100]

    if (AirFille == NULL || textFile)
    {
    printf("Failed to open file \n")
    return -1;

    else if ( text1[0] =='\n')
    { fgets(text1, 1000, AirFile);
    fprintf(textFile, "allocating:", AirFile);
    }
    }
    fclose(textFile);
    fclose(AirFile)'

    retrun 0;

    }
     
    Justme, Sep 30, 2006
    #1
    1. Advertising

  2. Justme

    Eric Sosman Guest

    Re: ow can i use fgets to read and ignore the first two lines ofa file and output into another file

    Justme wrote:
    > [...]
    > This is what my current code looks like
    > [...]


    Please post actual code, not an inaccurate "looks like"
    hazy approximation that won't even compile. All the time
    spent debugging your `retrun' statement and the other errors
    introduced by sloppy transcription is time not spent solving
    your actual problem.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Sep 30, 2006
    #2
    1. Advertising

  3. Justme

    Justme Guest

    Eric Sosman wrote:
    > Justme wrote:
    > > [...]
    > > This is what my current code looks like
    > > [...]

    >
    > Please post actual code, not an inaccurate "looks like"
    > hazy approximation that won't even compile. All the time
    > spent debugging your `retrun' statement and the other errors
    > introduced by sloppy transcription is time not spent solving
    > your actual problem.
    >
    > --
    > Eric Sosman
    > lid



    that is my actual code, that is why i'm trying to get help.
     
    Justme, Sep 30, 2006
    #3
  4. Justme

    pete Guest

    Justme wrote:
    >
    > Novice programmer needs help with using fgets to read and ignore the
    > first two lines of a file. I've gone thru the previous posting
    > regarding fgets, but none of them seems to help my situation.
    > I have airdata file that i have to read, but in other teh fscanf to
    > work properly, i need to ignore the first two lines,
    > because scanf does
    > not read spaces.
    >
    > This is what my current code looks like
    >
    > #include <stdio.h>
    > #include <sting.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > FILE * AirFile; //the file that contains the data to be read
    > FILE *textFile; //the out file for both lines
    >
    > char text1[1000];
    > char text2[100]
    >
    > if (AirFille == NULL || textFile)
    > {
    > printf("Failed to open file \n")
    > return -1;
    >
    > else if ( text1[0] =='\n')
    > { fgets(text1, 1000, AirFile);
    > fprintf(textFile, "allocating:", AirFile);
    > }
    > }
    > fclose(textFile);
    > fclose(AirFile)'
    >
    > retrun 0;
    >
    > }


    That might be a sketch artist's impression of you code,
    but that's not what your code looks like.

    There is no standard header called <sting.h> in C.
    There is no "retrun" keyword in C.
    Your "else" is not associated with a previous "if".
    You don't make any attempt to open the files.

    --
    pete
     
    pete, Sep 30, 2006
    #4
  5. Justme

    Malcolm Guest

    "Justme" <> wrote in message
    news:...
    >
    > Eric Sosman wrote:
    >> Justme wrote:
    >> > [...]
    >> > This is what my current code looks like
    >> > [...]

    >>
    >> Please post actual code, not an inaccurate "looks like"
    >> hazy approximation that won't even compile. All the time
    >> spent debugging your `retrun' statement and the other errors
    >> introduced by sloppy transcription is time not spent solving
    >> your actual problem.
    >>
    >> --
    >> Eric Sosman
    >> lid

    >
    >
    > that is my actual code, that is why i'm trying to get help.
    >

    Synatx errors are not the same as logic errors, and are usually picked up by
    the compiler at compile time. All keywords must be spelt correctly, as must
    all identifiers.

    As for ignoring two lines, that is no problem. Simply call fgets() twice
    with a big buffer, and ignore the result. Strictly you should check for read
    errors, but it's probably best to leave that at present.

    --
    www.personal.leeds.ac.uk/~bgy1mm
    freeware games to download.
     
    Malcolm, Oct 1, 2006
    #5
  6. Justme

    Eric Sosman Guest

    Re: ow can i use fgets to read and ignore the first two lines ofa file and output into another file

    Justme wrote:
    > Eric Sosman wrote:
    >
    >>Justme wrote:
    >>
    >>>[...]
    >>>This is what my current code looks like
    >>>[...]

    >>
    >> Please post actual code, not an inaccurate "looks like"
    >>hazy approximation that won't even compile. All the time
    >>spent debugging your `retrun' statement and the other errors
    >>introduced by sloppy transcription is time not spent solving
    >>your actual problem.
    >>
    >>--
    >>Eric Sosman
    >>

    >
    > that is my actual code, [...]


    Then it is beyond my poor powers to debug. Have a
    nice life!

    --
    Eric Sosman
    lid
     
    Eric Sosman, Oct 1, 2006
    #6
  7. Justme

    BWIGLEY Guest

    "Justme" <> wrote in message
    news:...
    >
    > Eric Sosman wrote:
    > > Justme wrote:

    <snip>
    > that is my actual code, that is why i'm trying to get help.
    >


    Well here's a the code with proper formatting but I havn't done
    anything concerning files and can't be bothered looking it up for you
    but maybe without all the other errors you'll be able to figure it
    out:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>

    int main() {

    FILE *AirFile; /*the file that contains the data to be read*/
    FILE *textFile; /*the out file for both lines*/

    char text1[1000];
    char text2[100];

    if (AirFile == NULL || textFile == NULL) {
    printf("Failed to open file \n");
    return -1;

    if ( text1[0] =='\n') {
    fgets(text1, 1000, AirFile);
    fprintf(textFile, "allocating:", AirFile);
    }
    }

    fclose(textFile);
    fclose(AirFile);
    return 0;
    }
     
    BWIGLEY, Oct 1, 2006
    #7
  8. "Justme" <> writes:

    > Novice programmer needs help with using fgets to read and ignore the
    > first two lines of a file. I've gone thru the previous posting
    > regarding fgets, but none of them seems to help my situation.
    > I have airdata file that i have to read, but in other teh fscanf to
    > work properly, i need to ignore the first two lines, because scanf does
    > not read spaces.
    >
    > This is what my current code looks like
    >
    > #include <stdio.h>
    > #include <sting.h>
    > #include <stdlib.h>
    >
    > int main()
    > {
    > FILE * AirFile; //the file that contains the data to be read
    > FILE *textFile; //the out file for both lines


    Files never got opened.

    > char text1[1000];
    > char text2[100]
    >
    > if (AirFille == NULL || textFile)


    AirFile and textFile are not initialised thus the condition is true or
    false depending on position of the Sun.

    > {
    > printf("Failed to open file \n")
    > return -1;
    >
    > else if ( text1[0] =='\n')


    Unexpected "else". Plus text1[0] is uninitialised (so again condition
    depends on position of the stars) plus it (sort of) checks for empty
    lines only.

    > { fgets(text1, 1000, AirFile);
    > fprintf(textFile, "allocating:", AirFile);
    > }
    > }
    > fclose(textFile);
    > fclose(AirFile)'
    >
    > retrun 0;
    >
    > }


    Here's the way to do it with stdin/stdout:

    #v+
    #include <stdio.h>

    int main(void) {
    char buffer[1024];
    size_t num = 2;

    while (num && fgets(buffer, sizeof buffer, stdin)) {
    const char *ch = buffer;
    while (*ch && *ch!='\n') ++ch;
    if (*ch) --num;
    }

    while (!feof(stdin) && !ferror(stdin) &&
    (num = fread(buffer, 1, sizeof buffer, stdin)) &&
    num == fwrite(buffer, 1, num, stdout));

    return 0;
    }
    #v-

    --
    Best regards, _ _
    .o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
    ..o | Computer Science, Michal "mina86" Nazarewicz (o o)
    ooo +--<mina86*tlen.pl>--<jid:mina86*jabber.org>--ooO--(_)--Ooo--
     
    Michal Nazarewicz, Oct 1, 2006
    #8
  9. Michal Nazarewicz said:

    <snip>

    > Here's the way to do it with stdin/stdout:
    >
    > #v+


    No, that's just a syntax error.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
     
    Richard Heathfield, Oct 1, 2006
    #9
  10. Justme

    clayne Guest

    Michal Nazarewicz wrote:
    > "Justme" <> writes:
    >
    > > Novice programmer needs help with using fgets to read and ignore the
    > > first two lines of a file. I've gone thru the previous posting
    > > regarding fgets, but none of them seems to help my situation.
    > > I have airdata file that i have to read, but in other teh fscanf to
    > > work properly, i need to ignore the first two lines, because scanf does
    > > not read spaces.

    >
    > while (num && fgets(buffer, sizeof buffer, stdin)) {
    > const char *ch = buffer;
    > while (*ch && *ch!='\n') ++ch;
    > if (*ch) --num;
    > }
    >
    > while (!feof(stdin) && !ferror(stdin) &&
    > (num = fread(buffer, 1, sizeof buffer, stdin)) &&
    > num == fwrite(buffer, 1, num, stdout));


    Really no need to make it so complicated for 2 initial lines... i.e.
    fread() is already heading towards pre-optimization territory, where
    you might as well use mmap() (not ansi C however) if you have it.

    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>

    int main(int argc, char **argv)
    {
    FILE *in;
    char buf[128];
    size_t i;

    if (argc <= 1 || (in = fopen(argv[1], "r")) == NULL)
    return -1;

    for (i = 0; i < 2 && fgets(buf, sizeof(buf), in); )
    if (strchr(buf, '\n')) i++;

    while (fgets(buf, sizeof(buf), in)) {
    /* won't get here unless we've read 2 lines */
    fprintf(stdout, "%s", buf);
    }

    fclose(in);

    return 0;
    }
     
    clayne, Oct 1, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Wright
    Replies:
    0
    Views:
    555
    Joe Wright
    Jul 27, 2003
  2. Replies:
    13
    Views:
    2,236
    josh logan
    Aug 30, 2008
  3. Replies:
    8
    Views:
    179
    Sven-Thorsten Fahrbach
    Jul 28, 2005
  4. PerlFAQ Server
    Replies:
    0
    Views:
    180
    PerlFAQ Server
    Jan 14, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    168
    PerlFAQ Server
    Apr 19, 2011
Loading...

Share This Page