fscanf into an array

Discussion in 'C Programming' started by totoro2468@gmail.com, Nov 5, 2006.

  1. Guest

    Hi, I'm a complete NOOB.
    How do I get fscanf to copy into an array? I also need to use malloc,
    but where do I put it in my file?
    _______________________________________________________
    FILE *ifp;

    char c;
    int y = 0;

    txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;

    printf ("Enter the name of the file to analyze : ");
    gets (filename);
    printf ("\n");
    ifp = fopen(filename, "r");

    if (ifp == NULL)
    {
    fprintf (stderr, "Error opening file\n");
    exit (-2);
    }
    while(fscanf (ifp, "%c", &c) != EOF)
    {
    txtFile[y] = c;
    y++;
    }
     
    , Nov 5, 2006
    #1
    1. Advertising

  2. said:

    > How do I get fscanf to copy into an array?


    One element at a time.

    > I also need to use malloc,
    > but where do I put it in my file?


    At the point where you first need the memory. Be sure to #include <stdlib.h>
    for the malloc prototype.

    > FILE *ifp;
    >
    > char c;
    > int y = 0;
    >
    > txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;


    txtFile = malloc(FILELENGTH * sizeof *txtfile);
    if(txtFile != NULL)
    {

    >
    > printf ("Enter the name of the file to analyze : ");
    > gets (filename);


    Don't use gets. It's a buffer overflow waiting to happen. Instead, use
    fgets, find the newline using strchr, and overwrite it with '\0' if it's
    found. (If it's not found, you probably don't want to accept the filename
    anyway.)

    > printf ("\n");
    > ifp = fopen(filename, "r");
    >
    > if (ifp == NULL)
    > {
    > fprintf (stderr, "Error opening file\n");
    > exit (-2);
    > }
    > while(fscanf (ifp, "%c", &c) != EOF)


    I recommend == 1 rather than != EOF. fscanf returns the number of fields
    successfully converted, and you're expecting one field, so that's what you
    should test for.

    > {
    > txtFile[y] = c;
    > y++;
    > }


    This is fine provided y never equals or exceeds the array bound. But, to
    answer your question(!), you can instead do this:

    y = 0;
    while(y < FILELENGTH && fscanf(ifp, "%c", &txtFile[0]) == 1)
    {
    y++;
    }

    if(y == FILELENGTH)
    {
    you ran out of buffer, but at least no harm was done.
    }

    If you plan to use txtFile as a string, don't forget to null-terminate it,
    which entails leaving space for a null terminator:

    y = 0;
    while(y < FILELENGTH - 1 && fscanf(ifp, "%c", &txtFile[0]) == 1)
    {
    y++;
    }

    if(y == FILELENGTH - 1)
    {
    you ran out of buffer, but at least no harm was done.
    }
    else
    {
    txtFile[y] = '\0';
    }

    --
    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, Nov 5, 2006
    #2
    1. Advertising

  3. Guest

    How do I use fgets?




    Richard Heathfield wrote:
    > said:
    >
    > > How do I get fscanf to copy into an array?

    >
    > One element at a time.
    >
    > > I also need to use malloc,
    > > but where do I put it in my file?

    >
    > At the point where you first need the memory. Be sure to #include <stdlib.h>
    > for the malloc prototype.
    >
    > > FILE *ifp;
    > >
    > > char c;
    > > int y = 0;
    > >
    > > txtFile = (char *) malloc(FILELENGTH * sizeof(char)) ;

    >
    > txtFile = malloc(FILELENGTH * sizeof *txtfile);
    > if(txtFile != NULL)
    > {
    >
    > >
    > > printf ("Enter the name of the file to analyze : ");
    > > gets (filename);

    >
    > Don't use gets. It's a buffer overflow waiting to happen. Instead, use
    > fgets, find the newline using strchr, and overwrite it with '\0' if it's
    > found. (If it's not found, you probably don't want to accept the filename
    > anyway.)
    >
    > > printf ("\n");
    > > ifp = fopen(filename, "r");
    > >
    > > if (ifp == NULL)
    > > {
    > > fprintf (stderr, "Error opening file\n");
    > > exit (-2);
    > > }
    > > while(fscanf (ifp, "%c", &c) != EOF)

    >
    > I recommend == 1 rather than != EOF. fscanf returns the number of fields
    > successfully converted, and you're expecting one field, so that's what you
    > should test for.
    >
    > > {
    > > txtFile[y] = c;
    > > y++;
    > > }

    >
    > This is fine provided y never equals or exceeds the array bound. But, to
    > answer your question(!), you can instead do this:
    >
    > y = 0;
    > while(y < FILELENGTH && fscanf(ifp, "%c", &txtFile[0]) == 1)
    > {
    > y++;
    > }
    >
    > if(y == FILELENGTH)
    > {
    > you ran out of buffer, but at least no harm was done.
    > }
    >
    > If you plan to use txtFile as a string, don't forget to null-terminate it,
    > which entails leaving space for a null terminator:
    >
    > y = 0;
    > while(y < FILELENGTH - 1 && fscanf(ifp, "%c", &txtFile[0]) == 1)
    > {
    > y++;
    > }
    >
    > if(y == FILELENGTH - 1)
    > {
    > you ran out of buffer, but at least no harm was done.
    > }
    > else
    > {
    > txtFile[y] = '\0';
    > }
    >
    > --
    > 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)
     
    , Nov 5, 2006
    #3
  4. writes:
    > How do I use fgets?


    Please don't top-post. Read the following:

    http://www.caliburn.nl/topposting.html
    http://www.cpax.org.uk/prg/writings/topposting.php

    Please don't quote the entire article to which you're replying; quote
    only the portions that are relevant to your followup.

    How do you use fgets()? You start by reading its description in
    whatever textbook or tutorial you're using to learn C, or by reading
    the system documentation ("man fgets" on some systems) to find out how
    it's used.

    We can answer questions here, but we can't teach you the entire
    language. The most valuable think you can learn is how to find out
    these things for yourself.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
     
    Keith Thompson, Nov 5, 2006
    #4
    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. cylin
    Replies:
    7
    Views:
    3,913
    Kevin Goodsell
    Sep 19, 2003
  2. Marc Reclaire

    Question about fscanf ..

    Marc Reclaire, Dec 18, 2003, in forum: C++
    Replies:
    2
    Views:
    391
    Karl Heinz Buchegger
    Dec 18, 2003
  3. John Phung

    fscanf equivalent in c++

    John Phung, Apr 6, 2004, in forum: C++
    Replies:
    11
    Views:
    19,312
    Nick Hounsome
    Apr 9, 2004
  4. Fallon

    fscanf question

    Fallon, Jul 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    389
    Giuseppe
    Jul 13, 2003
  5. Chris Torek
    Replies:
    0
    Views:
    401
    Chris Torek
    Jul 14, 2003
Loading...

Share This Page