mardi 30 juin 2015

How to read text file in C?


I'm trying to read a txt file containing strings of 1s and 0s and print it out in the manner below. I tried my code a couple of months ago and it worked fine in reading the text file. Now when I tried it, it outputs something really strange. Also I tried changing the directory of the file to a non-existant file but it still outputs the same thing when it should've quit the program immediately. Please help!

The content of txt file:-

10000001
01110111
01111111
01111010
01111010
01110111

Expected output:-

data_in<=24'b10000001;
#10000;

Real output:-

data_in<=24'b(some weird symbol that changes everytime I recompile);
#10000;

My code:-

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

int main(int argc, char *argv[])
{
int i,j;
j = 0;
char words [50];
FILE* fp;
fp = fopen(argv[1], "r");

if(fp==NULL)
{

printf("Can't open file\n");
}


while(feof(fp)==0)
{
fscanf(fp,"%s", words);
printf("data_in<=24'b%s\n", words);
printf("#10000\n");


}

fclose(fp);
system("PAUSE");
return 0;
}

The input argument is the following:-

"C:\Users\Beanz\Documents\MATLAB\football frame\frame1.txt"


Aucun commentaire:

Enregistrer un commentaire