1

running UNIX through putty, and using vi and gcc. I am attempting to read in a line of a file and using a ragged array store the lines. I won't provide the whole code because it is unnecessary,

main(){
   char func[51];
   char * functions[201];
   FILE * inf;
   if (( inf = fopen("test.txt", "r")) == NULL){
       printf("can't open so exiting\n");
       exit(EXIT_SUCCESS)


   int i;
   i = 0;
   while( fscanf(inf, "%s", func) != EOF){
        functions[i] = func;
        i++;
   }
   printf("%s\n", *functions); /* this is just for me to check if its working*/

   }

incldues stdio.h, stdlib.h and string.h

SO the code works in that it runs through the file and during the while loop it stores the line but I want it so that functions[0] stores a pointer leading to the first line and functions[1] stores a pointer leading to the second line and so on. I'm not sure how to do this, any help would be appreciated thanks :)

Patrick Johnston
  • 101
  • 1
  • 1
  • 12

3 Answers3

2

You need to assign a copy of func to functions[i], as in functions[i] = strdup(func), if you have strdup.

lhf
  • 70,581
  • 9
  • 108
  • 149
  • I changed it to strdup(func), But now it seems to only be storing the first line, as that is all that is being printed out. – Patrick Johnston May 16 '11 at 03:42
  • using `strdup` means you will also have to `free` the memory before the program exits. – DipSwitch May 17 '11 at 11:42
  • @DipSwitch, all memory is freed automatically when the program exits. See http://stackoverflow.com/questions/2213627/when-you-exit-a-c-application-is-the-malloc-ed-memory-automatically-freed – lhf May 17 '11 at 11:43
  • @Patrick, *you* are just printing the first line. Your last line of code does that. It does *not* print the whole list. – lhf May 17 '11 at 11:46
  • Quote from the link 'Relying on this is bad practice and it is better to free it explicitly. The issue isn't just that your code looks bad. You may decide you want to integrate your small program into a larger, long running one. Then a while later you have to spend hours tracking down memory leaks. Relying on a feature of an operating system also makes the code less portable.' – DipSwitch May 17 '11 at 11:51
2

Your problem is that you are reusing the func array - all functions[i] point to the same place and therefore will contain the same string (the last one you read). You will need to allocate enough memory for all your different strings.

Have a look at this similar question.

Community
  • 1
  • 1
hugomg
  • 68,213
  • 24
  • 160
  • 246
0

Please fix your formating / code blocks now it seems like you are reading test.txt when you have failed to open the file and exited the program.

You have to keep in mind that the variable func points to the data structure func[51] now when you fscanf you overwrite func[51] all the time with a new line. And since your are storing the address pointing to func[51] to in the array functions you don't actually store the contents of the char array (func[51]).

You could solve this by make your functions array an two-dimensional array char functions[201][51] and then use a strcpy to copy the data structure func is pointing to to functions[i] with strcpy(functions[i], func);

this will result in something like:

main() {
    char func[51];
    char functions[201][51];
    FILE * inf;
    if (( inf = fopen("test.txt", "r")) == NULL) {
        printf("can't open so exiting\n");
        exit(EXIT_SUCCESS)
    } // <-- you are missing this one

    int i = 0;
    while( fscanf(inf, "%s", func) != EOF){
        strcpy(functions[i++], func);
    }
    printf("%s\n", *functions); /* this is just for me to check if its working*/
}

This could also be:

while (fscanf(inf, "%s", functions[i++]) != EOF);
DipSwitch
  • 5,470
  • 2
  • 20
  • 24