Read File with comma and Bar seperators Output in Console.

Hello everyone,

I have a file named "A6.txt" inside it has this code:

1
2
3
4
5
6
April Joe, 1, SUPER DUPER ULTRA SECRET, 02031982|
Matthews Jocob, 2, TOP SECRET, 11031992|
Garfield Kitty, 3, SECRET, 04041942|
Lake Bill, 4, MEH, 12031968|
Jones Betty, 5, WATCHLIST, 06031974|
Goodman Betty, 6, BANE OF SOCIETY, 05021952|


Very Simple, all it has is "Name, ID, Access, Date of Birth" (DOB needs to be formatted like 00/00/0000 if possible)

Output should Look like :
1
2
3
4
Client #1:
     Name: April Joe
     Access:  SUPER DUPER ULTRA SECRET
     DoB: 02/03/1982


I tried looking for many tutorials online none of which did this. If anyone can help me with this simple issue I have I would be grateful.
So far I got most of the code to work, but now i need to develop a Numb to Date Converter.

Here is what i got so far,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <cstdlib>
#include <string>
#include <fstream>
#include <iostream>

std::string clean(std::string the_string)
	{
		char bad[] = {'.', ',', ' ', '|', '\0'};
		std::string::size_type n = 0;

		while ((n=the_string.find(bad[0], n))!=the_string.npos)
		{
			the_string.erase(n,1);
		}

		n = 0;
		while ((n=the_string.find(bad[1], n))!=the_string.npos)
		{
			the_string.erase(n,1);
		}

		n = 0;
		while ((n=the_string.find(bad[2], n))!=the_string.npos)
		{
			the_string.erase(n,1);
		}

		n = 0;
		while ((n=the_string.find(bad[3], n))!=the_string.npos)
		{
			the_string.erase(n,1);
		}
			
		return the_string;
}

int main(){
	
   char chars[] = "|";
    std::ifstream infile("A6.txt");

    std::string item,id,code,dob;
	
    getline(infile,dob,'|');
    while (!infile.eof()){
    getline(infile,item,',');
    getline(infile,id,',');
    getline(infile,code,',');
    getline(infile,dob);
        
		std::cout << "Name:" << item << "\n";
		std::cout << "id:" << id << "\n";
		std::cout << "Clearence:" << code << "\n";
		std::cout << "DoB:" << clean(dob) << "\n\n";
		
    }

    return EXIT_SUCCESS;
}



I need it to print out "02/03/1982" instead of 02031982 on this line:
std::cout << "DoB:" << clean(dob) << "\n\n";


Any Help?
Get the date as a std::string from your file and pass it through this function.

1
2
3
4
5
6
7
8
9
10
std::string parseDate(std::string d_str) {
  return std::string (
    // Month
      d_str.substr(0,2) + "/"
    // Day
    + d_str.substr(2,2) + "/" 
    // Year
    + d_str.substr(4,4)
  );
};


It's pretty self explanatory.
Last edited on
If you're allowed to use libraries I'd suggest using boost for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <boost/algorithm/string/split.hpp>

void myFunc() {
 string input = "April Joe, 1, SUPER DUPER ULTRA SECRET, 02031982|";

 vector<string> line_parts;
 boost::split(line_parts, input, boost::is_any_of(","));

 cout << "Name: " << line_parts[0] << endl;
 cout << "Id: " << line_parts[1] << endl;
 cout << "Clearance: " << line_parts[2] << endl;
 cout << "DoB: " << parseDate(line_parts[3] << endl;
}
Last edited on
closed account (18hRX9L8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/* Made by usandfriends */
 
 #include <cstdio>
 
 main()
 {
 	FILE *infile;
 	int count,nscan;
 	char name[20],clientnum[5],access[35],dob[20],termch;
 	
 	infile=fopen("A6.txt","r");
 	
 	while(true)
 	{
 		nscan=fscanf(infile,"%20[^,], %5[^,], %35[^,], %20[^|]|%c",name,clientnum,access,dob,&termch);
 		if(nscan==EOF)
 		{
 			break;
 		}
 		printf("Client #%s:\n     Name: %s\n     Access: %s\n     DoB: %s\n\n",clientnum,name,access,dob);
 	}
 	fclose(infile);
 }


I'm sorry, can't help you on the birthdate.
Last edited on
Topic archived. No new replies allowed.