Simple C program to read a file line by line

Viewed 11599

What I would like to do is read the whole first line of the file but then after the first line only read the following lines until whitespace is hit. My end goal is to ask the user what line they want to edit by adding/subtracting time to said line.

Sample File

My test file
00:19.1 123456
00:35.4 testing whitespace end

Desired Output

1: My test file
2: 00:19.1
3: 00:35.4

Code:

#include <stdio.h>
#include <stdlib.h>

int main()
{
    FILE *fptr1, *fptr2;

    char filechar[40];
    char c[50];
    int line_number = 1;
    int replace_line, temp = 1;

    printf("Please enter a file name: ");
    scanf("%s", &filechar);

    if ((fptr1 = fopen(filechar, "r")) == NULL)
    {
        printf("Error locating desired file");
        exit(1);
    }

    c = getc(fptr1);
    while (c != EOF)
    {
        //printf("%d: %c",line_number, c);
        printf("%s",c);
        c = getc(fptr1);
        //line_number++;
    }
    return 0;
}
2 Answers
Related