1

ThisAnd thisI am trying to get the things written in a .txt file called CodeHere.txt and here is my main.cpp:

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


int main(int argc, const char * argv[]) {
    string line;
    string lines[100];
    ifstream myfile ("CodeHere.txt");
    int i = 0;
    if (myfile.is_open())
    {
      while ( getline (myfile,line) )
      {
          lines[0] = line;
          i++;
          
      }
      myfile.close();
    }

    else cout << "Unable to open file";
    
    cout << lines[0];
    
    myfile.close();

    return 0;
}

And the output is: Writing this to a file.Program ended with exit code: 0

But in my CodeHere.txt it has: hello

I tried saving it, but the result didn't change. I'm not sure whats going on. Can anyone help?

23
  • "And the output is: Writing this to a file...". I don't see how that's possible. Please show the exact code that you're running. Commented Aug 23, 2020 at 21:32
  • I put the pictures one is auto showing and for the other click the link. @cigien Commented Aug 23, 2020 at 21:38
  • lines[i] = line; cout<<lines; but we are just reading from a file. Commented Aug 23, 2020 at 21:40
  • I can't reproduce. Seems to work fine. Are you sure you're compiling and running the correct program? I don't see where Writing this to a file is could come from. Commented Aug 23, 2020 at 21:40
  • 1
    @Omer: yes I believe it is the text file. Try not to copy and paste but type Hello yourself into a new text file, perhaps there is some weird end-of-line invisible symbol your editor inserts into the txt file that ifstream is not able to recognize. Commented Aug 23, 2020 at 22:13

3 Answers 3

1

Are you sure that your .txt file is in the same repertory? To me, it just looks like you entered the path wrong. Try with the absolute path (full one). Another option is that you haven't saved the text file yet, you're just editing it, and so it is in fact empty, that would be why your cout doesn't print anything.

Sign up to request clarification or add additional context in comments.

3 Comments

It was probably reading the wrong file. I was messing around before this and may have created a invisible file which it thought it should read. When i gave the full path it worked
@Omer: the text file should be in the same folder of the c++ project, if you want to avoid having to give the full path to ifstream
@Omer I'm glad you got it figured out! If you want to use a relative path, be carefull. Depending on your IDE, the path may end up relative to your source code (as intended) or relative to the compiled code (probably not in the same directory /!\).
1

This should work, using a vector<string> to store the lines read from file

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(int argc, const char * argv[]) {
    string line;
    vector<string> lines;
    ifstream myfile ("CodeHere.txt");
    int i = 0;
    if (myfile.is_open())
    {
        while ( getline(myfile, line) )
        {
             lines.push_back(line);
             i++;       
        }
        myfile.close();
    }
    else {
        cout << "Unable to open file";
        return -1;
    }

    cout << lines[0] << '\n';

return 0;
}

6 Comments

Nope , I think it's a bug, it shows the exact same thing. There must be a problem with the file or xcode. And also did you notice there is a question mark next to the txt file, do you know what that means?
Test the program with some other text file. It works fine on my computer, reads any first line of any .txt file I tried with success.
Both your code and the OP's code will only output the first line that was read in.
If you are using std::vector, you don't need the i variable.
@ThomasMatthews: you are right in both instances, the code will output the first line only, and the i is useless, but I am not the author so I don't know whether this will be used in some ways that will require to leave the oddities where they are.
|
1

Try this:

  vector<string> lines;
  if (file.is_open()) {
    // read all lines from the file
    std::string line;
    while (getline(file, line)) {
      lines.emplace_back(line);
    }
    file.close();
  }
  else {
    cout << "Unable to open file";
    return -1;
  }
  cout << "file has " << lines.size() << " lines." << endl;
  for (auto l : lines) {
    cout << l << endl;
  } 

1 Comment

file has 1 lines. Writing this to a file. Program ended with exit code: 0 still doesn't work I'm gonna use another IDE i think there is a problem with Xcode

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.