How do you open a file using a string from an array? - c++

I have an array of file names that I need to open. When I put in the plans.open. It gives me the error "no matching function for call to 'std::basic_ifstream::open(std::__cxx11:..."
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using namespace std;
int main(){
ifstream files;
ifstream plans;
string stufiles[100];
int numFiles,timeBlocks;
files.open("filesToProcess.txt");
if (files.fail()){ //checks to see if the selected store file opened
cout << "Error when opening file!" << endl;
return 0;
}
files >> numFiles;
for (int i= 0; i<= numFiles; i++) {
files >> stufiles[i];
}
files.close();
cout << stufiles[0] << endl;
plans.open(stufiles[0]);
if (plans.fail()){ //checks to see if the selected store file opened
cout << "Error when opening file!" << endl;
return 0;
}
}
This is supposed to open the file using the file name in the array.
It gives me the error "no matching function for call to 'std::basic_ifstream::open(std::__cxx11:..."

Your compiler's version of std::ifstream::open() does not support std::string as input, so you will have to give it a const char* instead. You can use std::string::c_str() for that:
plans.open(stufiles[0].c_str());

Related

Cannot open text file using ifstream

ifstream fin;
fin.open("‪C:\\Users\\Zach\\Desktop\\input.txt");
if (!fin)
{
cout << "e";
}
e is printing whether I use the full pathway or just input.txt from a resource file
If the file exists, make sure that you have got the path specified correctly. Since you're running on Windows, you can verify the full path to your executable with the following code.
#include <iostream>
#include <fstream>
#include <string>
#include <windows.h>
#define BUFSIZE 4096
std::string getExePath()
{
char result[BUFSIZE];
return std::string(result, GetModuleFileName(NULL, result, BUFSIZE));
}
int main()
{
std::ifstream infile("input.txt");
if (infile.is_open())
{
std::cout << "Success!" << std::endl;
infile.close();
}
else
{
std::cout << "Failed to open input.txt!" << std::endl;
std::cout << "Executable path is ->" << getExePath() << "<-" << std::endl;
}
return 0;
}
This will allow you to verify that your path to the input file is correct, assuming that it's collocated with your executable.
You need to direct output into the ifstream object by using fin << "string"; and not directing to standard out via cout.

when i open a file, nothing will be displayed - C++

Premise: I'm using CLion.
As i said in title, when i try to open a file (txt) nothing will be displayed.
i can't explain it, i don't think i made an error, it's pretty easy this code:
#include <iostream>
#include <cstdlib>
using namespace std;
int main() {
FILE *leggi;
leggi = fopen("lorem.txt", "r");
char datiLetti[1000];
while(fgets(datiLetti, 1000, leggi)!=NULL){
cout << datiLetti << endl;
}
fclose(leggi);
system("PAUSE");
return EXIT_SUCCESS;
}
file "lorem.txt" is in the same directory of the project.
Thank you in advance.
EDIT1: file is lorem not lorem_ipsum, my mistake when i typed here.
You want this:
...
FILE *leggi;
leggi = fopen("lorem.txt", "r");
if (leggi == NULL)
{
cout << "Can't open file" << endl;
return 1;
}
...
---FIXED---
Installed cygwig1.dll and cygstdc++-6.dll and put cygwig in glob variables, then my file worked in the same directory of main and exe.
However, thank you guys for your time!
fopen is a C solution for open a file if you want to open a file in c++ use fstream like flowing code.
fopen is deprecated in c++11.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string line;
fstream myfile;
myfile.open("example.txt");
cerr << "Error: " << strerror(errno);
if (myfile.is_open())
{
while (getline(myfile, line))
{
cout << line << '\n';
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}

Read multiple files with C++

I would like to edit the below code to look at and read several other files in the proc directory. May I get some guidance on how to improve this code to look at other proc files other than just the uptime. Thank you.
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib> // for exit()
int main()
{
using namespace std;
// ifstream is used for reading files
// We'll read from a file called Sample.dat
ifstream inf("/proc/uptime");
// If we couldn't open the input file stream for reading
if (!inf)
{
// Print an error and exit
cerr << "Uh oh, file could not be opened for reading!" << endl;
exit(1);
}
// While there's still stuff left to read
while (inf)
{
// read stuff from the file into a string and print it
std::string strInput;
getline(inf, strInput);
cout << strInput << endl;
}
return 0;
// When inf goes out of scope, the ifstream
// destructor will close the file
}
Here it is written with a function instead
#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib> // for exit()
using namespace std;
void readfile(string file)
{
ifstream inf (file.c_str());
if (!inf)
{
// Print an error and exit
cerr << "Uh oh, file could not be opened for reading!" << endl;
exit(1);
}
while (inf)
{
std::string strInput;
getline(inf, strInput);
cout << strInput << endl;
}
}
int main()
{
cout << "-------------------obtaining Totaltime and Idletime----------------" << endl;
readfile("/proc/uptime");
return 0;
}

std::getline fails at reading file via ifstream

I created this piece of code (using C++11 standards):
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter the name of a file:\n";
getline(cin, input);
cout << "Reading file...\n";
ifstream readstream(input);
if (!readstream.is_open()) {
cout << "Error while opening file\n";
return 0;
} else {
string currentln;
while (getline(readstream, currentln)) {
cout << currentln;
}
return 0;
}
}
I compiled this code with the mingw-w64 implementation of GCC, with this command:
gcc -std=c++11 read.cpp -o read.exe
It compiled successfully, however, when I run it:
Enter the name of a file:
example.txt
Reading file...
And then nothing. It doesn't output any characters of the file. The file does exist, and it doesn't have any problems opening it. However, when I compile and run this code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter the name of a file:\n";
getline(cin, input);
cout << "Reading file...\n";
ifstream readstream(input);
if (!readstream.is_open()) {
cout << "Error while opening file\n";
return 0;
} else {
char currentchar;
while (!readstream.eof()) {
while(readstream.get(currentchar)) {
cout << currentchar;
}
}
return 0;
}
}
It works.
I compiled it the same way:
g++ -std=c++11 read.cpp -o read.exe
But instead of using getline(), I used ifstream.get();
Can anyone tell me why getline() does not work in this situation?

ifstream not working

I'm trying to open a file using ifstream, but no matter what solutions I find that I've tried, nothing seems to work; my program always outputs "unable to open". Below is my code in its entirety. Any help at all is appreciated!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char ** argv)
{
string junk;
ifstream fin;
fin.open("somefile.txt");
if(fin.is_open())
{
fin >> junk;
cout << junk;
}
else
{
cout << "unable to open" << endl;
}
fin.close();
return 0;
}
Also, the contents of somefile.txt, which is in the same directory as the created executable is the following:
SOME
FILE
As some commenters have suggested, it could easily be that the file truly doesn't exist, because you're looking for it in the wrong place. Try using an absolute path to the file rather than just assuming it's looking where you expect.
And output a more helpful error message using strerror(errno).
// ...
fin.open("C:\\path\\to\\somefile.txt");
// ...
else
{
cout << "unable to open: " << strerror(errno) << endl;
}