input/output from files

Hi guys. So I was trying to take some names from a file and sort them but when I try to get the lines from the original .txt my string n_name comes empty. Can someone help pls?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  int main()
{
	string loc;
	string loc_f;
	vector<string> names;
	string n_name;
	ifstream in_stream;
	ofstream out_stream;

	cout << "loc?\n";
	getline(cin, loc);
	loc_f = loc + "_sorted.txt";
	
	in_stream.open(loc);

	while (getline(in_stream, n_name))
	{
		names.push_back(n_name);
	}

	bubblesort(names);

	in_stream.close();

	out_stream.open(loc_f);
	
	for (size_t i = 0; i < names.size(); i++)
	{
		cout << names[i] << endl;
		out_stream << names[i] << endl;
	}

	out_stream.close();

	return 0;
}
Last edited on
Hello cibide,

The short answer is that line 16 is trying to read a file that is not open.

On line 11 you get user input for the variable "loc" on line 12 you set loc_f equal to "loc" plus a file name. Then on line 14 you use "loc" to open the file when you should have used "loc_f", so you never open the file. Line 16 does not work because there is no open file, so "n_name" does not receive a value.

I find it useful to check that the file has been opened before proceeding with the program. I like to use this code, but there are other ways:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
std::ifstream inFile;

inFile.open(iFileName); // iFileName would compare to your loc_f. I define and initialize this variable
	               // at the beginning of main or the function s it can be easily changed.

if (inFile.is_open())
{
	//  When working you can comment out these lines.
	std::cout << "\n File " << iFileName << " is open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));  // Needs the header files chrono and thread.
}
else
{
	std::cout << "\n File " << iFileName << " did not open" << std::endl;
	std::this_thread::sleep_for(std::chrono::seconds(3));
	exit(1);  //  Exit the program because nothing can be done.
}


Once you know that the file is open things should work better.

You can work "iFileName" to be a "path + fileName" combination.

I will also mention your use of the "_" in variable names and your file name is acceptable, but not considered the best way of writing names. As soon as I can find a link to better explain this I will post it.

Hope that helps,

Andy
have a check in your program that the file has been opened successfully
loc_f = loc + "_sorted.txt";
loc should have a file extension already at it's end, so while not strictly necessary to keep things clean removed that before the assignment to loc_f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <utility>
#include <algorithm>

int main()
{
	std::string loc{};
	std::cout << "loc? \n";
	getline(std::cin, loc);

	std::vector<std::string> names{};
    std::ifstream in_stream{loc};

	if(in_stream)
    {
        std::string line{};
        while (getline(in_stream, line))
        {
            if (in_stream)
            {
                names.push_back(std::move(line));
            }
       }
    }
    else
    {
        std::cerr << "Unable to open file \n";
    }
   // for (const auto& elem : names)std::cout << elem << "\n";
    std::sort(names.begin(), names.end());

    std::string loc_f = loc.substr(0, loc.size()-4) + "_sorted.txt";
    std::ofstream out_stream{loc_f};
    for (const auto& elem : names)
    {
        out_stream << elem << "\n";
    }
}
Last edited on
thank you both for answering.
loc is a direction to a file, like "...//names", and loc_f is the name of the new file that will have the names sorted, so I still don't understand why it is not open... do I have to put the file extension? it doesn't work like I wrote on the exemple??
And pls post that link :)
Hello cibide,

Here are some links that should prove useful:

http://www.cplusplus.com/forum/beginner/50422/
http://www.cplusplus.com/forum/lounge/108615/
http://www.cplusplus.com/forum/beginner/50929/
http://www.cplusplus.com/forum/beginner/133853/#msg716564
http://www.cplusplus.com/forum/general/95813/

This ones a bit funny, but has some good tips on variable names and "{}":
http://www.cplusplus.com/forum/lounge/60726/

This is a place to start, but not quite what I wanted. I have been reading for the last year so sometimes it is hard to remember where I read something.

You can always do a search here. I tried "naming variables" for the above links.

Hope that helps,

Andy
I still don't understand why it is not open... do I have to put the file extension

yes, for e.g. in windows 10 if I have a file test1.txt in the F folder, my code for direct initialization of the std::ifstream object would would be:
 
std::ifstream inFile{"F:\\test1.txt"};

I suppose you tried running the if(in_stream) test suggested and that's how you found out your file was not opening? the only thing i can think of is the std::string loc is not matching the full file-name such as described above.
thank you, guys!
Topic archived. No new replies allowed.