Janae
Janae

Reputation: 65

how to read characters and integers from a file?

So I have a data file that looks something like this:

x + y + z

30 45 50

10 20 30

The only characters I needed was the operators, so '+' '+' I was able to use file.get() to successfully get those characters and put them in an array. Problem is I need to get the next line of numbers, and assign them to the values x , y z . I know I cant use .get() , I would have to use getline. Will i have to eliminate the file.get() and use getline instead for first part also?

I looked at some of the questions posted on here but none of them were quite like mines. Note I'm actually going to be using these values for another part of my program, just used cout to see if my values were being read correctly Here's my previous code:

main(int argc, char *argv[])
{
    int a=0;
    int n;
    fstream datafile;
    char ch;
    pid_t pid;
    int a, b, c, result;
   

    string line;
    datafile.open("data1.txt");

    if(datafile)
    {
      for(int i=0; i <9; i++)
      {     
        datafile.get(ch);
      
        if (ch == '*'||ch == '/'||ch == '+'||ch == '-')
        {
           operations[a] = ch;
           cout<<operations[a];
           a++;
        }

      }
    }

    else
        cout<<"Error reading file"; 
}

So this is how I was getting the first line of the file in the beginning. It worked like I wanted it to, may have not been the nicest coding but it worked. Nevertheless I tried to get the rest of the file, this time using getline, but instead of getting the numbers I was getting a bunch of random gibberish/numbers. I know if I use getline, the first line cannot be in my loop. I know this is how I would get the numbers.

 while(getline(datafile, line))
 {
   istringstream  ss(line);
   ss >> x >> y >> z;
   cout<<x<<""<<y<<""<<z;
 }

Upvotes: 4

Views: 7638

Answers (2)

IllusiveBrian
IllusiveBrian

Reputation: 3204

Would the following make sense for the first line, or am I missing something:

string input;
std::getline(datafile, input)
for (int i = 0; i < input.size(); i++)
    if (input[i] == '+' || ...)
    {
        operations[a] = input[i];
        a++;
    }

If you don't want to use getline, you could simply read the entire file stream (note that the bool is a rather naive way to handle the problem, I'd recommend something more elegant in your actual code):

bool first = true;
string nums;
int lines = 0;
vector<vector<int>> numlines;
vector<int> topush;
while (!datafile.eof())
{
char ch = datafile.get()
if (ch == 12 && first) //I don't know if '\n' is valid, I'd assume it is but here's the sure bet
    first = false;
else if (first && (ch == '+' || ...))
{
    operator[a] = ch;
    a++;
}
else if (!first && (ch >= '0' && ch <= '9'))
{
    if (!(datafile.peek() >= '0' && datafile.peek() <= '0'))
    {
         numlines[lines].push_back(atoi(nums.c_str());
         nums.clear();
         if (datafile.peek() == 12)
         {
             numlines.push_back(topush);
             lines++;
         }
    }
    else
        nums = nums + ch;
}

Honestly, I can't be sure the above will work exactly right, I'd recommend you just modify your code to use getline exclusively. You'll need to add #include to get atoi.

Upvotes: 2

yizzlez
yizzlez

Reputation: 8805

Add this to your code:

while(!datafile.eof()){
    string s;
    getline(datafile, s);
    istringstream in(s);
    string tmp;
        while(in >> tmp){
            int i = stoi(tmp) 
            //Do something with i....
        }
}

Upvotes: 0

Related Questions