0

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
Paul Sanders
  • 24,133
  • 4
  • 26
  • 48
  • 2
    In C++ files are usually read by `std::ifstream`, using `FILE*` is more the C way. Use that and you're almost done in your case (hint: `operator>>`). – nada Nov 02 '20 at 11:58
  • Should check also that the file was opened successfully. – Paul Sanders Nov 02 '20 at 12:02
  • Which C++ textbook are you using, which gives this as an example of reading from a file? Whichever textbook it is, you should seriously consider throwing it away and getting a better textbook that correctly explains how to read and write files in modern C++. – Sam Varshavchik Nov 02 '20 at 12:19

2 Answers2

0

You are writing C code, tag your question with "c" is more fitted. You code looks ok, except that you didn't activate the function display_birthday. I tested your code in C, and has to change some head files, and activate the birthday printing, remove the using namespace, and save the code as a .c file.

#include <stdio.h>
#define MAX_CHAR 65500
#include <stdlib.h>
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);
    display_birthday(ids, count);
    return 0;
}

You were working in a over-heavy way. Since the input file is a txt file. You may input each line into a string, and working line by line. Retrieve the whole text file into a single array is a very odd thought.

enter image description here

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
ytlu
  • 412
  • 4
  • 9
0

Since you want a c++, I give the following typical c++ code for your reference.

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::string str;
    int year;
    char schar str2[3]={}, month[3]={}, day[3]={};
    std::ifstream file("idnumbers.txt");
    while ( std::getline(file, str) )
    {
      if (str[0] == '_')
      {
          str.copy(str2, 2, 1); // copy year into str2
          year = std::stoi(str2);  // convert to integer
          if (year > 20) year += 1900;
          else year += 2000;
          str.copy(month, 2, 3); // copy two month bytes
          str.copy(day, 2, 5);   // copy two day bytes
          std::cout << "Date born: " << year << "/" << month <<"/" << day << std::endl;
       }
    }
    file.close();
    return 0;
}
ytlu
  • 412
  • 4
  • 9