read in text file, store as string/int/char

Can somebody tell me how read in a text file like this? I am using a combination of string, int, and char variables. Normally, when I have read in files like this in the past, I have not used strings, just char arrays.

Example Data that I'm reading in from a text file where each line has the following format:
string name; char flag; string tag; string contactInfo; int count; string message%

Inigo Montoya;e;email;fiveFingeredMan@gmail.com;0;
Bruce Smith;m;messenger;SourPatchSmith;0;
Dolly Llama;e;email;DramaLlama@gmail.com;1;Hello, this is a reminder that you have an appointment Monday at 3pm. Please give at least 24 hours notice to cancel.%
Mark LaRusso;e;text;251-333-4562;1;What time is the soccer game?%
Martha Epp;m;messenger;MeppHead;2;Thank you, Thomas. I am available Wednesday 8-9:30 am or 11:30-1:30 pm. Thursday, I'm open any time before 3 pm.%Thanks Thomas, that will work perfectly. See you then.%
Jane Lawson;e;email;JaneAndTarzan@gmail.com;1;Good afternoon, hope this message finds you well. This is just a reminder that we have an appointment tomorrow morning.%

Normally, this is how I have approached this:

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
ifstream inFile;
char name[30];
char flag; // single character
char tag[15];
char contactInfo[100];
int count = 0;
char message[1000];

// read in first line
inFile.get(name, 30, ';');
while(!inFile.eof())
{
 inFile.ignore(30, ';');
 inFile >> flag;
 inFile.ignore(100, ';');
 inFile.get(tag, 15, ';');
 inFile.ignore(15, ';');
 inFile.get(contactInfo, 100, ';');
 inFile.ignore(100, ';');
 inFile >> count;
 inFile.ignore(100, ';');

 // so on and so forth
 // I have conditional statements based on my flag ('e', 't', 'm')
 //   and a separate set of conditions based on the count, if 0, no messages
 //   if count != 0, read in messages separated by '%' as delimeter

}


try to avoid while ! eof, it can be tricky to get right.
instead say while (file >> data)

you have the right idea, string and char array don't matter here (string is better but the syntax is the same).

you can use alternative characters for getline. if you used a ; instead, would that work?
alternately you can read one giant line and split it on the ;s using find and substring string functions.

are the ones with a message still all one long line in the text file? Or do you have end of lines in that mix?
Last edited on
Hello FeedMeTacos,

Not sure all of what you want to do, but this code:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

#include <fstream>

int main()
{
    const std::string inFileName{ "Input.txt" };  // <--- Put File name here.

    std::ifstream inFile(inFileName);

    if (!inFile)
    {
        std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
        //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

        return 1;
    }

    std::string name;
    char flag{}, junk{};
    std::string tag;
    std::string contactInfo;
    int count;
    std::string message;

    while (std::getline(inFile, name, ';'))
    {
        inFile >> flag >> junk;
        std::getline(inFile, tag, ';');
        std::getline(inFile, contactInfo, ';');
        inFile >> count >> junk;
        std::getline(inFile, message);

        std::cout
            << "\n"
            << "Name: " << name << '\n'
            << "flag: " << flag << '\n'
            << "Tag: " << tag << '\n'
            << "Contact Info: " << contactInfo << '\n'
            << "Count: " << count << '\n'
            << "Message:\n" << message << '\n';
    }

	// <--- Keeps console window open when running in debug mode on Visual Studio. Or a good way to pause the program.
	// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
	//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // <--- Requires header file <limits>.
	std::cout << "\n\n Press Enter to continue: ";
	std::cin.get();

	return 0;  // <--- Not required, but makes a good break point.
}


Produced this output:


Name: Inigo Montoya
flag: e
Tag: email
Contact Info: fiveFingeredMan@gmail.com
Count: 0
Message:


Name: Bruce Smith
flag: m
Tag: messenger
Contact Info: SourPatchSmith
Count: 0
Message:


Name: Dolly Llama
flag: e
Tag: email
Contact Info: DramaLlama@gmail.com
Count: 1
Message:
Hello, this is a reminder that you have an appointment Monday at 3pm. Please give at least 24 hours notice to cancel.%

Name: Mark LaRusso
flag: e
Tag: text
Contact Info: 251-333-4562
Count: 1
Message:
What time is the soccer game?%

Name: Martha Epp
flag: m
Tag: messenger
Contact Info: MeppHead
Count: 2
Message:
Thank you, Thomas. I am available Wednesday 8-9:30 am or 11:30-1:30 pm. Thursday, I'm open any time before 3 pm.%Thanks Thomas, that will work perfectly. See you then.%

Name: Jane Lawson
flag: e
Tag: email
Contact Info: JaneAndTarzan@gmail.com
Count: 1
Message:
Good afternoon, hope this message finds you well. This is just a reminder that we have an appointment tomorrow morning.%


 Press Enter to continue:


I do not know what you want to d with the (%) in the message.

It is an idea for a start.

The question I have is do you just need to read the file or do you need to save what you read for future use in the program?

Andy
I had initially tried to use getline, but I didn't realize I wasn't using it correctly. Got it up and running!

As far as the message field goes, I'm actually probably going to swap out the '%' for ';' I was initially trying to differentiate those fields from the others while importing the file, but now that I have a message counter, I don't need to do anything different with the messages.

Thanks!
Hi Andy,

I was initially using '%' to differentiate between the other fields and the messages while the file was reading input. I changed the data file and added an extra field that contains the number of messages for each contact. If the count is not 0, then the program enters a loop and extracts the messages. It it is 0, then it just goes on to read in other data. Since I am using count to determine if I need to check for messages, I can just use the same delimeter I was using for the rest of the file.
Topic archived. No new replies allowed.