0

Hi. Can you help me read a two digit like "12" from an input.txt file, is it advisable to use fscanf or fgets?
This is a sample of the txt file :
a(1,4,3,12,15)

Thanks :)

2
Contributors
3
Replies
7
Views
5 Years
Discussion Span
Last Post by blue_Student
0

It's generally advisable to use fgets() to retrieve a line, then parse the line in memory, possibly with sscanf(). But the needs of your program override general advice, and it looks a lot like you need to tokenize a function call for a compiler.

What does the file represent, and what are you doing with the data?

0

we we're asked to read the numbers then equate them with their BCD equivalents, the numbers in the input file is from 0 to 15 :)

0

I have done some code:

#include<stdio.h>

#define SIZE 30

int main() {

    char A[SIZE];
    int x;
    int count;

    FILE *filePtr = fopen("input.txt", "r");

    filePtr = fopen("input.txt", "r");

    if(filePtr == NULL) {
        printf("The file can't be opened \n");
    }
    else{
        while((fgets(A, SIZE, filePtr)) != EOF) { //gets the line
            sscanf(A, "%d", x); //converts the char into integers (hope so)
            A[count] == x; //store integer in an array
            count++;
        }
    }

    fclose(filePtr);
    return 0;
}

When I compiled it, there is a warning at line 19, and says "comparison between pointer and integer"

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.