You are here:

C/"fscanf" and "fgets" problems again.

Advertisement


Question
-------------------------
Followup To
Question -
I got a exercise with C language. I've got to find an error in part of C code. My program is trying to read from text file and print results on the screen.
The text file contains some people names, separated by empty fields " ". The program reads the text data with fscanf function and next puts read names into array of strings. But when the whole read procedure finishes, the array is filled with last read name from text file! I don't know why fscanf or fgets override previous array positions with last read name. I'm trying to fix it for over three days. Please help!
I can send You the C source code and text file if You need.
Answer -
YES.
Post the C source code and also the sample text file.
I will try to help you out.

-ssnkumar

HERE IS THE C SOURCE CODE AND EXAMPLE CONTENTS OF TEXT FILE:

#include <stdio.h>

int main(void)
{

FILE *my_file;
char *data, *array[10];
int indexer, condition;

  my_file=fopen("names.txt", "r");
  indexer=0;

  while(condition!=EOF)
  {
     condition=fscanf(my_file, "%s", data);
     if(condition!=EOF)
     {
        array[indexer]=data;
        printf("Array position: %d, name: %s.\n",
        indexer, array[indexer]);
        indexer++;
     }   
  }
  fclose(my_file);
  
  printf("\n\n");
  for(indexer=0;indexer<=10;indexer++)
  {
     printf("Array position: %d, name: %s.\n",
     indexer, array[indexer]);
  }
  
  return 0;

}

/*FILE "names.txt" contains as follows:

Mark Mike Anna Cindy John Adam Tom Eva Jack Art Bill

*/  

Answer
Here is the corrected code:
#include <stdio.h>

int main(void)
{
       FILE *my_file;
       char *data, *array[10];
       int indexer, condition = 1;

       my_file = fopen("names.txt", "r");
       indexer = 0;

       data = (char *) malloc(25);

       while(condition != EOF)
       {
         condition = fscanf(my_file, "%s", data);
         if(condition != EOF)
         {
         array[indexer] = (char *) malloc(25);
         strcpy(array[indexer], data);
         printf("Array position: %d, name: %s.\n", indexer, array[indexer]);
         indexer++;
         }
       }
       fclose(my_file);

       printf("\n\n");
       for(indexer=0;indexer<=10;indexer++)
       {
         printf("Array position: %d, name: %s.\n",
         indexer, array[indexer]);
       }

       return 0;
}

The mistakes that you had done are:
1. Using uninitialized variable (ex: condition)
2. Using pointers without allocating memory.
3 Instead of copying data, you were just giving the address of data to the array. And this address always remains the same and hence all the array members point to same name!

-ssnkumar

C

All Answers


Answers by Expert:


Ask Experts

Volunteer


Narendra

Expertise

I can answer questions in C related to programming, data structures, pointers and file manipulation. I use Solaris for doing C code and if you have questions related to C programming on Solaris, I will be able to help better.

Experience

6.5

Organizations belong to
Sun Microsystems

Awards and Honors
Brain Bench Certified Expert C programmer.
Advanced System Software Certified

©2012 About.com, a part of The New York Times Company. All rights reserved.