2

I have a file named read.txt E:\My_project\dictionary database\read.txt and it looks like

1245
15
12
454564
122
....

I want to read the read.txt line by line and want to save these value into a vector and finally output the vector and write the values of vector into another txt file named write.txt which will be look same as read.txt?? How can i do this in C++???

I tried to read the value from file like this:

  ifstream ifs("read.txt", ifstream::in);

But i dont understand where to keep the read.txt file.What should be the location of read.txt and write.txt???

Edited: if i use vector to save the input from the text i m getting an error:

int textLine;
  vector<int> input;

  ifstream ifs("C:\\Users\\Imon-Bayazid\\Desktop\\k\\read.txt", ifstream::in);

  if (ifs.good())   {

        while (!ifs.eof()) {
              getline(ifs, textLine);
              input.push_back(textLine);
        }

        ifs.close();

  } else
      cout << "ERROR: can't open file." << endl;


        for(int i=0;i<input.size();i++)
          cout<<input.at(i);
ImonBayazid
  • 1,066
  • 3
  • 16
  • 41

3 Answers3

1

If your binary is located in E:\My_project, then you need to adjust the path of the file you are opening to: ifstream ifs("./dictionary database/read.txt", ifstream::in);

See this related question.

Community
  • 1
  • 1
Lander
  • 3,369
  • 2
  • 37
  • 53
1

Since the name of the file is hardcoded to "read.txt", the file must be in the same folder as your executable. If the location of the file is not going to change, you can hardcode full path:

ifstream ifs("E:\\My_project\\dictionary database\\read.txt", ifstream::in);

(note the doubled backslashes: that's for the C++ compiler to treat them as regular slashes).

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • I have edited my question could you please see this to answer ?? @dasblinkenlight – ImonBayazid Mar 02 '13 at 09:15
  • 1
    @user2029069 You get an error because you cannot use `getline(ifs, textLine);` to read an `int`. You need to use `ifs >> textLine` for that. I suggest renaming `textLine` to something more appropriate, e.g. `intValue`. – Sergey Kalinichenko Mar 02 '13 at 09:18
  • 1
    @user2029069 the same thing applies: use `<<` to output `int`s into a file, and don't forget to add `endl`. Something like `ofs << myInt << endl;` in a loop. – Sergey Kalinichenko Mar 02 '13 at 09:27
1

You can give the absolute path of the file when you open it:

ifstream ifs("E:\\My_project\\dictionary database\\read.txt", ifstream::in);

Or you you can move the executable program which reads the file in the same directory where the file is.

Edit:

By declaring your vector like this: vector<int> input; you create a vector in which you can store integer values only, but what you are reading from file(textLine) is a string. If you only want to interpret the numbers from file as integer values you will have to use

input.push_back(atoi(textLine.c_str()));
niculare
  • 3,629
  • 1
  • 25
  • 39