Help with reading from txt file

Hey guys, I need to get all the lyrics from my txt file into my program but unfortunately when I put a test run in there it keeps saying that the file is not found?? How to print out the txt file?
Btw, I get no errors. It just can't find the file for some reason.
And yes I have the txt file inside of the programs folder.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
if(trackNumber == 1){
            cout << "MAN ON THE SILVER MOUNTAIN" << endl;
            cout << endl;

            ifstream song1;
            song1.open("ManOnTheSilverMountain.txt");
            if(song1.is_open()){
            while(!song1.eof()){
                cout << words << endl;
                song1 >> words;
            }
            }else{
                cout << "Error opening file!" << endl;
            }
            song1.close(); 

 }
In order to open your input file the file must exist in the program's current working directory. Unfortunately the program's current working directory can vary depending on how you are running the program.

So please state your operating system, your IDE, if any.

OS: Ubuntu
IDE: CodeBlocks
Last edited on
Then the current working directory is usually where you project files are located.
True, but for some reason it still doesn't want to grab that txt file.... I'll play with it a bit and see if I can get it working.
If not I'll repost that issues are still occurring with no luck.
I've got Ubuntu running on a VM on my local machine, and I'm actually using Code::Blocks as well.

This is a little but of code to open my file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{

    std::ifstream _in;
    _in.open("text.txt");
    if (_in.fail())
    {
        std::cerr << "Failed to open" << std::endl;
        return(-1);
    }
    std::string _store;
    std::getline(_in, _store);

    std::cout << _store << std::endl;

    return 0;
}


And it outputs
kurt@kurt-VirtualBox:~/Desktop/CPPDCOM/bin/Release$ ./CPPDCOM
My Text


That was from executing it from the terminal, and my text file was in the same folder as the executable. Executing it in Debug from inside Code::Blocks pulled the text file from the same folder that "main.cpp" was in.
I even tried moving the "folder" that has the txt file in it to the src file. Thinking maybe that would work but no luck! I have the txt file in a folder because there is going to be MANY txt files that need to be.. compressed/ put in one place to keep organized. Without putting it into a folder there would be MANY txt files all through the programs location folder.
It keeps saying, "error opening file". So... I'm taking it for some reason it isn't accessing the txt file. Ideas?
Last edited on
Hmm. That's totally weird.

You could always provide the full file path, if nothing else works. Hmm.

Try polling the Current Working Directory and see what it is.
1
2
3
4
5
6
7
8
9
10
11
12
#include <unistd.h>
#include <stdio.h>

int main() {
   char dir[1024];

   if (getcwd(dir, sizeof(dir)) != NULL)
       fprintf(stdout, "Current working dir: %s\n", dir);
   else
       perror("getcwd() error");
   return 0;
}


kurt@kurt-VirtualBox:~/Desktop/CPPDCOM/bin/Release$ ./CPPDCOM 
Current working dir: /home/kurt/Desktop/CPPDCOM/bin/Release
How about riting...combo of reading and writing
@Jay
The issue with adding the pathway is would it work if the program is distributed? (Will be on others pc's, etc.)
@das2
I have no use for making it writeable, only readable. I created the txt file by just entering the info in the notepad and saving it. Instead of creating it all by coding. It's a Time saver.
Well, in a Release build the CWD is supposed to the same folder that the executable is in. Are you running it in debug right now? Yeah, hard-coding a path would make it impossible to distribute.

Did you look at what the little code snippet says the current working directory is?
Might I suggest a c++-Java mix program?? Always works for me
Yes I have the directory which is:
/home/codefreak/Desktop/C++ Projects/Lyrics

I moved the folder which is a old rock music band, Rainbow Band Info. out of the src folder but it is still in the program's folder (Lyrics).
I kind of figured that It might not work hard coding the dir path. So far I have 2 programs out there on the web that people can use (helpful) programs. So building software isn't new to me but this issue is the only thing stopping me from completing this piece of software.
That is really weird.

Alright. Every system call that generates an error will update 'errno'. See what errno says is the cause of the failure.
 
cerr << "Error: " << strerror(errno);
Hey Jay, do you possibly have teamviewer? I will be on tomorrow well... later on tonight considering it is 12:45am my time.
I'm going to try getting teamviewer to work on my Ubuntu and if you have the time we can tackle this irritating issue together.
I don't have teamviewer :/

But I have no classes tomorrow, so I'll be perusing here, trying to tell people to ignore the trolls, while doing some studying. I'll be up for a while tonight, but only because I'm just not tired. Either that or I'm so tired that I've gone beyond 'tired' and into 'hyper'.

You can send a PM or bump your post some time tomorrow and I should see it. I check my email often, so I'll be alerted to a PM quickly.
Found what the issue was. I didn't save the file name under ".txt" and when I was trying to call the file WITH the .txt it couldn't find it.
Lol, simple mistake that took hours. Got it up and working now.
Topic archived. No new replies allowed.