Good day, I am struggling to read a file from a .txt document, any help would be appreciated.
The code is almost done, it runs without errors, but it wont read the file where the data is stored.
the data is stored under the document: idnumbers.txt
the code:
using namespace std;
int year;
int read(char*);
void display(char*, int);
void display_birthday(char*, int);
int main()
{
char ids[MAX_CHAR];
int count=read(ids);
display(ids, count);
return 0;
}
int read(char*array){
FILE*file=fopen("idnumbers.txt","r");
printf("\nPrinting from file...\n");
char c;
int count=0;
for(int i=0;i<MAX_CHAR;i++){
if(fscanf(file,"%c",&c)==EOF){
break;
}
else {
*(array+i)=c;
count++;
}
}
return count;
}
void display(char*array, int count){
for(int i=0;i<count;i++){
printf("%c",*(array+i));
}
}
void display_birthday(char*array, int count){
for(int i=0;i<count;i++){
if(*(array+i)=='_'){
char word[3]={*(array+i+1),*(array+i+2)};
int year= atoi(word);
if (year<=20){
year+=2000;
}
else{
year+=1900;
}
printf("\nYear: %d/%c%c/%c%c",year,*(array+i+3),*(array+i+4),*(array+i+5),*(array+i+6));
}
}
}
The code takes the document named: idnumbers.txt and reads the content. Then it should print the date of birth of each line.
example output:
Date born: 1978/03/04
Date born: 1989/04/05
Date born: 1990/02/01
Date born: 1994/08/07
Date born: 1987/03/08
Date born: 1978/12/12
Date born: 2001/08/06
Date born: 2010/12/23
Date born: 2008/01/09
Date born: 1999/11/22
im not sure how to insert the .txt file into stackoverflow, so I just copied the content.
_7803045678087
_8904050876092
_9002017896054
_9408072345087
_8703083456098
_7812120867087
_0108068675087
_1012239687087
_0801090675086
_9911220768082