Reading file line by line
Hi guys i'm kind of new to C and am trying to read a text file that has a number stored on each line like below.
17767
9158
39017
18547
56401
23807
37962
22764
7977
This is the code i have so far, it's just attempting to read thefirst line then i plan to tak it from there, but when i run it i get some funny results rather than getting 17767 I get
177
18
91
Code:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
int main (void)
{
FILE *ptrFilein;
char cTempread;
int iLoop;
unsigned char ucBuffer[1000];
if((ptrFilein = fopen("random.txt","r"))==NULL)
{
printf("Error opening file");
return(-1);
}
cTempread = fgetc(ptrFilein);
for (iLoop = 0; fgetc(ptrFilein)!='\n'; iLoop++)
{
ucBuffer[iLoop] = cTempread;
cTempread = fgetc(ptrFilein);
}
printf("The values from the file were %s",&ucBuffer[0]);
}
Any help would be greatly appreciated Cheers
Chris