read and print from a file in C

Viewed 50

I am trying to read from a file which is from a .txt file containing this

CARD HOLDER NAME1  |  PRIMARY ACCOUNT NUMBER1  |  EXPIRATION DATE1  |  BALANCE1
CARD HOLDER NAME2  |  PRIMARY ACCOUNT NUMBER2  |  EXPIRATION DATE2  |  BALANCE2
CARD HOLDER NAME3  |  PRIMARY ACCOUNT NUMBER3  |  EXPIRATION DATE3  |  BALANCE3

and save it in the string line by referencing sp to it ...when i print sp in the function read_file("account data.txt", &line, 1, 2); ,I get the expected value which is third string in line 1 :

EXPIRATION DATE1

but by printing line after the function i get weird symbols ,the problem is probably in referencing ,so how to fix it ?

#include <stdio.h>
#include <string.h>

FILE* Data;

int read_file(char name[20], char* sp, int line_number, int order)
{
    char line[100];
    Data = fopen(name, "r");

    for (int i = 0; i < line_number; i++)
    {
        fgets(line, 100, Data);
    }

    sp = strtok(line, "|");
    
    for (int i = 0; i < order; i++)
    {
        sp = strtok(NULL, "|");
        
    }

    //printf("%s\n", sp);      gives me the expected value
}

int main()
{
    char line[100];

    read_file("account data.txt", &line, 1, 2);
    printf("%s", line);

    fclose(Data);
}

the output :

╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠╠öÉ3=°⌂

0 Answers
Related