-4

I am trying trying to read in a file in C++ where the characters are separated by bars "|", what is the best way to do that?

Thanks

awishn02
  • 39
  • 1
  • 5
  • 1
    [What have you tried?](http://mattgemmell.com/2008/12/08/what-have-you-tried/) – Some programmer dude Apr 24 '12 at 06:20
  • 2
    Welcome to StackOverflow. Can you provide a sample of the input you are describing? Also, how should newlines be treated in the input? As record separators, or as data inside a field? – Robᵩ Apr 24 '12 at 06:20
  • Read it in like you would any other file. Or rephrase your quesiton. – juanchopanza Apr 24 '12 at 06:21
  • Can you tell us if this is a homework assignment? We don't mind answering homework questions, but we answer them differently than work or play questions. – Robᵩ Apr 24 '12 at 06:27

3 Answers3

4

I assume that fields are separated by bars, but records are separated by newlines.

First, read in the lines of text using std::getline:

std::string line;
std::getline(std::cin, line);

Then, break the line at | boudaries.

std::stringstream sline(line);
std::string field;
std::getline(sline, field, '|');
...
std::getline(sline, field, '|'); 
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
  • OP, if you're reading in a file, use the name of your file stream instead of cin – Robotnik Apr 24 '12 at 06:25
  • @Robotnik, I concur. Also, if the OP indicates that this isn't homework, I'll put up a more complete code sample. – Robᵩ Apr 24 '12 at 06:26
1

Depending on the complexity of the code you're using, what you're looking for is a variation on a CSV (comma seperated value) file reader. There are thousands out there and if your code needs to be robust I'd suggest you use one of them rather than writing your own as there are complexities such as:

  • What if one of your 'values' actually contains a '|' character in quotes say?
  • What if one of your 'values' contains a carriage return or line feed?

One of the most popular is the Boost tokeniser which should be pretty easy to get up and working with. You just need to ensure that you tell the tokeniser that you're using a '|' as a field seperator.

Take a look at this related question for some more pointers.

Community
  • 1
  • 1
Component 10
  • 10,247
  • 7
  • 47
  • 64
0

Assume you have a file called "file.dat"
It looks like this:

item1|item2|item3|item4|item5|item6

This program will print each item in the file:

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

int main(){

    std::ifstream infile("file.dat");

    std::string item;
    getline(infile,item,'|');

    while (!infile.eof()){
        std::cout << item << std::endl;
        getline(infile,item,'|');
    }

    return EXIT_SUCCESS;
}
Trevor Hickey
  • 36,288
  • 32
  • 162
  • 271
  • I never recommend using `.eof()` as a loop condition. It almost always produces buggy code. Although your code seems to be the exception, I'd still recommend the easier-to-read: `std::string item; while(std::getline(infile, item, '|')) { std::cout << item << "\n"; }` – Robᵩ Apr 24 '12 at 13:56