fscanf into an array

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • totoro2468@gmail.com

    fscanf into an array

    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(FILELENG TH * 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++;
    }

  • Richard Heathfield

    #2
    Re: fscanf into an array

    totoro2468@gmai l.com 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(FILELENG TH * sizeof(char)) ;
    txtFile = malloc(FILELENG TH * 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

    email: rjh at above domain (but drop the www, obviously)

    Comment

    • totoro2468@gmail.com

      #3
      Re: fscanf into an array

      How do I use fgets?




      Richard Heathfield wrote:
      totoro2468@gmai l.com 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(FILELENG TH * sizeof(char)) ;
      >
      txtFile = malloc(FILELENG TH * 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

      email: rjh at above domain (but drop the www, obviously)

      Comment

      • Keith Thompson

        #4
        Re: fscanf into an array

        totoro2468@gmai l.com writes:
        How do I use fgets?
        Please don't top-post. Read the following:




        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_Keit h) kst-u@mib.org <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.

        Comment

        Working...