Khang
Khang

Reputation: 151

I think I declared and used arrays the wrong way

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

Upvotes: 3

Views: 112

Answers (2)

H.S.
H.S.

Reputation: 12669

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.

Upvotes: 2

Miket25
Miket25

Reputation: 1903

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]*/
    }
}

Upvotes: 5

Related Questions