1

I have a .txt file like this:

(A+B-C+6-8)
(A*5+4)

And I want to read each line as a string and put them in a string array. For example, str[10] would have the string "(A+B-C+6-8)" as its elements. Then I would do something with the array, then when I'm finished and there would be a new line, the array would reset and store the next line of strings.

What function should I use? fgets? fgetc? fscanf? I'm really confused with all these I/O functions. Thanks!

Dipto
  • 2,720
  • 2
  • 22
  • 42

2 Answers2

2

You should use fgets, to read one line at a time.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Get first line:

FILE* myFile = fopen("filename.ext", "r");
size_t maxNoChar = 100;
char * line = malloc(maxNoChar);
if(myFile != NULL)
{
    fscanf("%s", myFile, &line);
}
nestedloop
  • 2,596
  • 23
  • 34