Click here to Skip to main content
14,696,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
0
I have two text files like these:

11.txt:

1 5.66
2 4.95
3 2.55
4 0.99
5 2.87


NB.txt:

1 2 3 4 5
4 5 3 2 1
3 4 5 1 2

I have written the below code to fine, for example, "1" from File 1, and search it in File 2, then substitute "1" with "5.66". and repeat it for other numbers, i.e. 2,3,4,5. but I don't know why it doesn't work. additionally, it doesn't read the first line of 11.txt.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () 
{
    string  line;
    double AtomId, Atom_radius,search ;

    ifstream AtomId_file ("11.txt");
    string namefile;
    if (AtomId_file.is_open()){
        for (int linenox = 0; getline (AtomId_file,line) && linenox < 6; linenox++){
            if (linenox == 0)  AtomId_file>>AtomId>>Atom_radius;
            }
        cout<<"AtomId: "<<AtomId<<" Atom_radius: "<<Atom_radius<<endl;
        cout<<namefile<<"\n";

    }

    ifstream NB("NB.txt");

    size_t pos;
    if (NB.is_open())
      {     
          search = AtomId;
          getline(NB,line); 
          pos=line.find(search);
          if(pos!=string::npos) 
            {
                search = Atom_radius;
                cout <<"Found!";
            }
      } 

    ofstream myfile;
    myfile.open ("12.txt");
    myfile << search << "\n";
}


What I have tried:

the output in 12.txt is:

2

instead of :

5.66 4.95 2.55 0.99 2.87
0.99 2.87 2.55 4.95 5.66
2.55 0.99 2.87 5.66 4.95
Posted
Updated 4-Jan-20 7:11am
Comments
0x01AA 4-Jan-20 12:11pm
   
I would suggest you to do it the other way around.
Read item by item from "NB.txt" and search the respective key in "11.txt".
Finally write the result into a third file e.g. "Res.txt".

See notes below for points 1,2 and 3.
// ...
1.        for (int linenox = 0; getline (AtomId_file,line) && linenox < 6; linenox++){
            if (linenox == 0)  AtomId_file>>AtomId>>Atom_radius;
            }

    if (NB.is_open())
      {     
2.        search = AtomId;
          getline(NB,line); 
          pos=line.find(search);
          if(pos!=string::npos) 
            {
                search = Atom_radius;
                cout <<"Found!";
            }
      } 

    ofstream myfile;
    myfile.open ("12.txt");
3.    myfile << search << "\n";
}

1. You use getline to read the first line of 11.txt, then immediately read the next two tokens into AtomId and Atom_radius; that will be 2 and 4.95. What you should do after the getline, is to parse the string into the two separate fields.

2. Variable search now contains the double floating point value 2.0. You then read a line of text from NB.txt and search for that value in the string. But the string consists of characters which can never match a double value, so the call to line.find will fail. So pos will be equal to string::npos and the following block will be skipped. So variable search still contains the value 2.0.

3. You now open the output file and write the value of search into it. So the output file contains a single line with the string "2" as coded.

You need to start again with just a part of the code. First figure out how to read all the lines of the first file and split the tokens into separate strings (do not convert them to double types). Once you are certain that you have that part working you can figure out how to replace one string in the second file with its associated value from the first.
   
Quote:
I have written the below code to fine, for example, "1" from File 1, and search it in File 2, then substitute "1" with "5.66". and repeat it for other numbers, i.e. 2,3,4,5. but I don't know why it doesn't work.

Because your code is not trying to do what you say, you should reread you code.
The debugger will show you what is going on.

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   
Compiling does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900