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.

1
  • "and i can't get it to work with the second line" --> likely because the (unposted) code never read the first line's '\n'. Commented Jun 7, 2020 at 17:42

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

fscanf(fp, "%s \n", Students); fails to read a name with spaces in it like "Rohan Bari". No width limit. Like gets. the " \n" in "%s \n" serve no benefit here as the next "%s" consume the white space.
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
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.

2 Comments

This fails to read anything into Students if the first line is "\n". Without a width limit is is bad like gets(). It does not read the 2nd \n due to extraneous ].
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 <b>char</b> array they are being read into. Basically I make a myfgets() type function that does the safe read for any caller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.