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 :)

Recommended Answers

All 3 Replies

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?

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 :)

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"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.