Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - Let a C read a string of plastic numbers in txt with fread and store them in an array. The shorter the program, the better. Thank you.
Let a C read a string of plastic numbers in txt with fread and store them in an array. The shorter the program, the better. Thank you.
Fscanf is really suitable for this topic. If you have to use fread, I'll give you a paragraph.

# include & ltstdio.h & gt

# include & ltstdlib.h & gt

# include & ltstring.h & gt

int getNum(int *num,FILE* file)

{

char strNum[ 10];

char ch

int I = 0;

int nReadCount = 0;

memset(strNum,0x00,sizeof(strNum));

while(nReadCount = fread(& amp; ch,sizeof(char), 1,file))

{

if(ch & lt; 0 ' | | ch & gt'9')

{

* num = atoi(strNum);

Return nReadCount

}

other

{

strNum[i++]= ch;

}

}

* num = atoi(strNum);

Return nReadCount

}

int main()

{

FILE* file = fopen("a.txt "," r ");

int numArray[ 100];

int I = 0;

If (file! = empty)

{

while(getNum(& amp; numArray[i++],file))

{

If (i == 100)

{

Break;

}

}

}

for(int j = 0; J< me; j++)

printf("%d ",numArray[j]);

getchar();

}