Hi all,
I want to read a file.

The file is like that :
20 20 1 5 6
20 21 1 2 3
21 22 2 4 6
20 20 2 3 5
20 21 6 5 2
21 22 1 6 7
....


and I would like every time that I find number1 && number2 in a line to add number3
for example to print
20 20 1+2
20 21 1+6
21 22 2+1

Could you help me please do this ?

I know how to read file but I have not idea how can I do this ?

#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main() 
{
    float width,height,var1;
    ifstream inFile;
    inFile.open("data.txt");

    if (!inFile) 
    {
          cout << "Unable to open file";
          exit(1);
    }

    while(inFile)
    {
         inFile >> width >> height >> var1;
         
         cout << width << " " << height << " " << var1 << endl; 
    }
   


inFile.close();

system("pause");
return 0;
}

Recommended Answers

All 6 Replies

I think you should clarify what you are trying to do. Either your example output doesn't match your example input making it not make sense or your description of the required actions is too cryptic.

Example input Line 1:
20 20 1 5 6

Example output Line 1:
20 20 1+2

Where did the 5 and 6 go, and where did the 2 come from???

I think you should clarify what you are trying to do. Either your example output doesn't match your example input making it not make sense or your description of the required actions is too cryptic.

Where did the 5 and 6 go, and where did the 2 come from???

input
Line 1: 20 20 1 5 6
Line 2: 20 21 2 3 4
Line 3: 20 20 1 4 5
Line 4: 20 21 3 5 6

Line 1 variavle1=20 and variable2=20
I want to search all the file to find another Line that variable1=variable2=20
if it is, then I have to add the values of third variable.

Variable4 and Variable5 I don't care.

So

output
Line 1: 20 20 1+1
Line 2: 20 21 2+3

input
Line 1: 20 20 1 5 6
Line 2: 20 21 2 3 4
Line 3: 20 20 1 4 5
Line 4: 20 21 3 5 6

Line 1 variavle1=20 and variable2=20
I want to search all the file to find another Line that variable1=variable2=20
if it is, then I have to add the values of third variable.

Variable4 and Variable5 I don't care.

So

output
Line 1: 20 20 1+1
Line 2: 20 21 2+3

Someone to explain me the basic idea how to do it ? Please

Hmmm.... I'm thinking your going to have to use an array of custom data structures of some sort. That custom data structure will probably need definitions of operator== and operator+ as well as potentially some sort of input or read operation.

I don't think you'll be able to do this efficiently with any sort of Plain Old Data (POD). Do you know anything about structs and/or classes?

I'd do this:

read your data into std::list< std::vector< int > > data .

then you can use a series of loops like:

std::vector< int > allResults;

/* Go through all the lines, using an iterator */
std::list< std::vector < int > >::iterator it = data.begin();

while(it != data.end()){
   int result = 0;
   bool found_things = false;
   /* Check if the first two numbers of the current line are the same */
   if((*it).at(0) == (*it).at(1)){
      /* found at least one example */
      found_things = true;
      result += (*it).at(2);
      /* Now go through all the remaining lines to find matches */
      std::list< std::vector < int > >::iterator it_temp = it + 1;
      while(it_temp != data.end()){
         /* Check the first number first, only check the second if the first matches */
         if((*it_temp).at(0) == (*it).at(0)){
            /* found a match, so add it to the result */
            if((*it_temp).at(1) == (*it).at(1)){
               result += (*it_temp).at(2);
               /* Now remove this line, so that we don't repeatedly match it */
               data.erase(it_temp);
            }
            else{
               ++it_temp;
            }
         }
         else{
            ++it_temp;
         }
      }
   }
   /* If we found anything, add it to the list of all results */
   if(found_things)   allResults.push_back(result);
   /* move the iterator to the next line */
   ++it;
}

I think that should do it. a list is used instead of a vector, since deletion of elements from the middle of a list is way faster than for a vector. I hope that helps.

Thanks a lot all!
I did not know about vectors. I am new in C++ programming!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.