0

I'm trying to read a text file that will contain two lines, something like this:

18,3,4,c;19,3,5,D
19100,18,18;19102,3,2

and i want to store the first line in a string called Students and the second one into another string called Courses.

I have wrote this code but it stores one line only and i can't get it to work with the second line

FILE *fptr;
    if ((fptr = fopen("program.txt", "r")) == NULL) {
    printf("Error! opening file");
    exit(1);
    }
    fscanf(fptr, "%[^\n]", Students);
    fclose(fptr);

Can anyone help me with that? I'm a newbie to c and i can't get how to do so, Thank you in advance.

Abde
  • 21
  • 5

3 Answers3

2
FILE *fptr;
char buffer[255] = {'\0'};
    if ((fptr = fopen("program.txt", "r")) == NULL) {
    printf("Error! opening file");
    exit(1);
    }
    fgets(Students, sizeof(Students), fptr);
    fgets(Courses, sizeof(Courses), fptr);
    fclose(fptr);

This line fgets(Students, sizeof(Students), fptr); will start reading from the begginning of the file and store the first line to Students char array & then fgets(Courses, sizeof(Courses), fptr); will read the second line and store it into Courses char array.

Make sure that the size of Students & Courses is large enough to accommodate each line into them.

Shubham
  • 1,153
  • 8
  • 20
0

You may try fscanf() for the problem:

#include <stdio.h>

int main(void) {
    char Students[100], Courses[100];
    FILE *fp = fopen("program.txt", "r");

    if (!fp) {
        printf("File wasn't opened.\n");
        return -1;
    }

    fscanf(fp, "%s \n", Students);
    fscanf(fp, "%s", Courses);

    printf("%s\n", Students);
    printf("%s\n", Courses);

    fclose(fp);

    return 0;
}

My program.txt contains:

John_Doe
Mathematics

Sample Output

John_Doe    // Students
Mathematics // Courses
Rohan Bari
  • 7,482
  • 3
  • 14
  • 34
  • 1
    `fscanf(fp, "%s \n", Students);` fails to read a name with spaces in it like "Rohan Bari". No width limit. Like [gets](https://stackoverflow.com/q/1694036/2410359). the `" \n"` in `"%s \n"` serve no benefit here as the next `"%s"` consume the white space. – chux - Reinstate Monica Jun 07 '20 at 17:39
  • Thank you for your help, I tried it and tried the other codes in other comments and it worked just fine, honestly didn't expect that much help :D – Abde Jun 07 '20 at 17:39
0

Use something like:

   fscanf(fptr, "%[^\n]\n%[^\n]]\n", Students,Courses);

Where you tell scanf() to read up to a new-line, read and discard the new line, then do it again.

Gilbert
  • 3,740
  • 17
  • 19
  • This fails to read anything into `Students` if the first line is `"\n"`. Without a width limit is is bad like [gets()](https://stackoverflow.com/q/1694036/2410359). It does not read the 2nd `\n` due to extraneous `]`. – chux - Reinstate Monica Jun 07 '20 at 17:38
  • Kept with scanf() as it was in your example. For reading lines I tend to use direct fgets() in a manner that handles well lines longer than the char array they are being read into. Basically I make a myfgets() type function that does the safe read for any caller. – Gilbert Jun 10 '20 at 05:34