Arslan Ali
Arslan Ali

Reputation: 17802

Pull out data from a file and store it in strings in C++

I have a file which contains records of students in the following format.

Umar|Ejaz|12345|[email protected]
Majid|Hussain|12345|[email protected]
Ali|Akbar|12345|[email protected]
Mahtab|Maqsood|12345|[email protected]
Juanid|Asghar|12345|[email protected]

The data has been stored according to the following format:

firstName|lastName|contactNumber|email

The total number of lines(records) can not exceed the limit 100. In my program, I've defined the following string variables.

#define MAX_SIZE 100
// other code
string firstName[MAX_SIZE];
string lastName[MAX_SIZE];
string contactNumber[MAX_SIZE];
string email[MAX_SIZE];

Now, I want to pull data from the file, and using the delimiter '|', I want to put data in the corresponding strings. I'm using the following strategy to put back data into string variables.

ifstream readFromFile;  
readFromFile.open("output.txt");
// other code
int x = 0;
string temp;

while(getline(readFromFile, temp)) {
    int charPosition = 0;
    while(temp[charPosition] != '|') {
        firstName[x] += temp[charPosition];
        charPosition++;
    }
    while(temp[charPosition] != '|') {
        lastName[x] += temp[charPosition];
        charPosition++;
    }
    while(temp[charPosition] != '|') {
        contactNumber[x] += temp[charPosition];
        charPosition++;
    }
    while(temp[charPosition] != endl) {
        email[x] += temp[charPosition];
        charPosition++;
    }
    x++;
}

Is it necessary to attach null character '\0' at the end of each string? And if I do not attach, will it create problems when I will be actually implementing those string variables in my program. I'm a new to C++, and I've come up with this solution. If anybody has better technique, he is surely welcome.

Edit: Also I can't compare a char(acter) with endl, how can I?

Edit: The code that I've written isn't working. It gives me following error.

     Segmentation fault (core dumped)

Note: I can only use .txt file. A .csv file can't be used.

Upvotes: 0

Views: 408

Answers (2)

Thomas Matthews
Thomas Matthews

Reputation: 57678

There are many techniques to do this. I suggest searching StackOveflow for "[C++] read file" to see some more methods.

Find and Substring
You could use the std::string::find method to find the delimiter and then use std::string::substr to return a substring between the position and the delimiter.

std::string::size_type position = 0;
positition = temp.find('|');
if (position != std::string::npos)
{
    firstName[x] = temp.substr(0, position);
}

Upvotes: 4

Dietmar Kühl
Dietmar Kühl

Reputation: 153820

If you don't terminate a a C-style string with a null character there is no way to determine where the string ends. Thus, you'll need to terminate the strings.

I would personally read the data into std::string objects:

std::string first, last, etc;
while (std::getline(readFromFile, first, '|')
    && std::getline(readFromFile, last, '|')
    && std::getline(readFromFile, etc)) {
    // do something with the input
}

std::endl is a manipulator implemented as a function template. You can't compare a char with that. There is also hardly ever a reason to use std::endl because it flushes the stream after adding a newline which makes writing really slow. You probably meant to compare to a newline character, i.e., to '\n'. However, since you read the string with std::getline() the line break character will already be removed! You need to make sure you don't access more than temp.size() characters otherwise.

Your record also contains arrays of strings rather than arrays of characters and you assign individual chars to them. You either wanted to yse char something[SIZE] or you'd store strings!

Upvotes: 2

Related Questions