I have a file containing strings as well as numbers. For instance. my Store-1.txt file contains "coffee 2mug -4". I need a c program to store numbers (i.e. 2 and -4), reading a file and storing only numbers in an array.
I canβt figure out exactly how to do this. Any suggestions please.
code
#include <stdio.h> int main(void) { char c,ch; int flag=0; FILE *fptr=fopen("Store-1.txt","r"); if(fptr) { while((c=fgetc(fptr))!=EOF) { if(c=='-' || c== '+') { ch=c; flag=1; } if(c>='0' && c<='9') { if(flag == 1) { printf("%c",ch); flag =0; } printf("%c",c); } } } else printf("Error : file not found"); system("pause"); }
source share