3

I'm trying to create a simple C program that reads lines of string from a file then output it to the terminal, but apparently it keeps crashing and I don't know where I went wrong...I suspect that it might be the way that I'm handling the arrays in my code, because I'm still very new to C, so I'm still getting used to the ways that arrays are used and declared..

This is what my code currently looks like:

typedef struct my_string
{
  char str[256]; // my string contains an array of 255 characters + null
} my_string;

//Output lines from the file into terminal
void print(int count, my_string a[20]) {
    for (int i = 0; i < count; i++)
    {
        printf("%s\n", a[i]);
    }
}
//Read lines from file
void read(FILE *file_ptr) {
  int i;
  int numberOfLines;
  my_string lineArray[20];
  fscanf(file_ptr, "%d\n", &numberOfLines);
  for (i=0; i < numberOfLines; i++) {
     fscanf(file_ptr, "%[^\n]\n", lineArray[i].str);
  }
  print(numberOfLines, lineArray);
 }

void main()
{
  FILE *file_ptr;
// open the file and read from it
  if ((file_ptr = fopen("mytestfile.dat", "r")) == NULL)
    printf("File could not be opened");
  else {
    read(file_ptr);
  }
 fclose(file_ptr);
}

The text file that I'm trying to read from is this:

10
Fred
Eric
James
Jaiden
Mike
Jake
Jackson
Monica
Luke
Kai

Thanks

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Khang
  • 141
  • 1
  • 11

2 Answers2

5

A few things.

1) The return type for main is int and not void.

int main(){
   ...
}

2) If you couldn't open the file, then it shouldn't be closed. Move the fclose into the else statement.

else{
    read(file_ptr);
    fclose(file_ptr);
}

3) In your print function, make sure that the string is being printed and not the struct address. The compiler should've given a warning.

void print(int count, my_string a[20]) {
    for (int i = 0; i < count; i++)
    {
        printf("%s\n", a[i].str); /* a[i].str not a[i]*/
    }
}
Miket25
  • 1,895
  • 3
  • 15
  • 29
  • Thanks alot! That worked for me now. Just one question...Why is the return type for main is `int` and not `void`? I'm coming from a "pascal programming background", so to me `void` means it's a procedure and anything else other than that is a function with a `return` being a `result`.... – Khang Jun 03 '18 at 04:40
  • @Khang Usually zero is returned at the end of `main` to signify that the program ran with no errors. `int main()` and `int main(int argc, char** argv)` are part of the C standard. [More info here](https://stackoverflow.com/questions/204476/what-should-main-return-in-c-and-c) – Miket25 Jun 03 '18 at 04:48
2

One of the posts has been accepted as an answer to this question but it doesn't give the specific reason for the problem that OP has asked:

...but apparently it keeps crashing and I don't know where I went wrong.....

Hence I am posting this answer.

In your print(), you want to print the strings read from the file but you are giving a[i] as an argument to printf() which is of type my_string and using %s format specifier to print it. The %s format specifier expect the argument to be a pointer to the initial element of an array of characters. That means the argument my_string is not the correct type for %s which is a undefined behavior. An undefined behavior includes it may execute incorrectly (either crashing or silently generating incorrect results), or it may fortuitously do exactly what the programmer intended.

From C Standards#7.21.6.1p9

If a conversion specification is invalid, the behavior is undefined.282) If any argument is not the correct type for the corresponding conversion specification, the behavior is undefined.
[emphasis mine]

In your print() function you want to output the lines read from file, so the correct argument to printf() would be a[i].str.

void print(int count, my_string a[20]) {
    for (int i = 0; i < count; i++) {
        printf("%s\n", a[i].str);
    }
}

Since you are new to C programming, make yourself aware of the undefined behavior (if you are not familiar with it).

There are few other issues in your program which another answer is already pointed out.

H.S.
  • 11,654
  • 2
  • 15
  • 32
  • Wow.....C really doesn't do any work for you compared to Pascal, which does most things like this for you......Thanks alot! – Khang Jun 03 '18 at 07:16
  • @Khang: C does not protect you from yourself. Make sure you compile with as many warnings enabled as you can. – Jonathan Leffler Jun 03 '18 at 12:09