I'm pretty new to coding so I'm not entirely sure if I'm doing file extraction correct. I'm getting lldb as my output for this code. Instead of prompting the user with the words in the hangman.dat file.
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int main()
{
ifstream sourceFile;
sourceFile.open("hangman.dat");
if (sourceFile.fail())
{
cout<<"File didn't open" ;
}
else
{
string words;
sourceFile >> words;
while(sourceFile>>words)
{
cout<<words<<endl;
}
}
}
The file hangman.dat contains the following information:
Fall
leaves
Thanksgiving
pumpkins
turkey
Halloween
Related
When I compile my code all is good no errors but when I open the file that I am writing too I see that most of the text has been converted into strange Chinese looking characters.
#include <iostream>
#include <fstream>
#include <Windows.h>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string temp, line;
fstream file("LogData.csv");
fstream txtfile("temp.txt", ofstream::out, ofstream::trunc);
while (getline(file, line, '\n'))
{
txtfile << line;
}
file.close();
txtfile.close();
}
I am trying to simply read a file "input.txt" into an array people[]. The txt file has 3 numbers:
10
20
30
I am getting -9.25596e+61 instead of 10 for people[0]. Here is my code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
class Trip {
private:
double people[3];
public:
void readFile(string file);
};
void Trip::readFile(string file) {
ifstream input;
input.open(file);
input >> people[0] >> people[1] >> people[2];
cout << people[0];
input.close();
}
int main() {
Trip trip;
trip.readFile("input.txt");
return 0;
}
Your program is correct. Working fine.
Currently, it is not getting the file. So, it is failing for you.
Provide the fully qualified path to readFile() as below example:
Path like "C:\\Users\\source\\Temp\\x64\\Debug\\input.txt"
Windows support file path with backward or forward slashes:
msdn.microsoft.com/en-us/library/aa365247(VS.85).aspx
So this is a fairly simple example of a program where I'm trying to output the first two lines of an input text file. The ifstream should be a global variable, and the testGetFile() function is necessary (I have not done the actual text processing needed in this code.) I'm trying to figure out why this is cout-ing only the SECOND line of the input file. Any help will be appreciated!
Thanks in advance!
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
using namespace std;
ifstream input;
string testGetFile(){
string result;
getline(input,result);
return result;
}
int main(){
input.open("testInput.txt");
cout<< testGetFile();
cout<< testGetFile();
return 0;
}
I want to read the content of a file with c++. Im using ifstream, but here is an error at compiling:
Code:
#include <Python.h>
#include <iostream>
#include <fstream>
using namespace std;
ifstream script;
script.open("main.py");
const char *programm;
if (script.is_open()) {
while (!script.eof()) {
script >> programm;
}
}
script.close();
And the Error:
main.cpp:8:1: error: 'script' does not name a type
script.open("main.py");
^
main.cpp:10:1: error: expected unqualified-id before 'if'
if (script.is_open()) {
^
I hope you can help me, thanks!
#include <Python.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream script;
script.open("main.py");
// const char *programm; // You don't need a C string unless you have a reason.
string programm;
if (script.is_open()) {
while (!script.eof()) {
string line;
script >> line;
line += '\n';
programm += line;
}
}
script.close();
// Now do your task with programm;
return 0;
}
There are a few issues. The main one (causing the error) is that in C++ you can't just have code living on its own. It all goes into functions. In particular, you have to have the main function.
Also, your read loop is not going to work correctly. You should read into a std::string, that will keep track of the memory for you, and the current way you would miss the last string. I would suggest reading a line at a time. Something like this:
#include <Python.h>
#include <iostream>
#include <fstream>
#include <string>
int main()
{
std::ifstream script ("main.py");
std::string line;
if (!script) // Check the state of the file
{
std::cerr << "Couldn't open file\n";
return 1;
}
while (std::getline(script, line)) // Read a line at a time.
// This also checks the state of script after the read.
{
// Do something with line
}
return 0; // File gets closed automatically at the end
}
As somebody who is new to C++ and coming from a python background, I am trying to translate the code below to C++
f = open('transit_test.py')
s = f.read()
What is the shortest C++ idiom to do something like this?
The C++ STL way to do this is this:
#include <string>
#include <iterator>
#include <fstream>
using namespace std;
wifstream f(L"transit_test.py");
wstring s(istreambuf_iterator<wchar_t>(f), (istreambuf_iterator<wchar_t>()) );
I'm pretty sure I've posted this before, but it's sufficiently short it's probably not worth finding the previous answer:
std::ifstream in("transit_test.py");
std::stringstream buffer;
buffer << in.rdbuf();
Now buffer.str() is an std::string holding the contents of transit_test.py.
You can do file read in C++ as like,
#include <iostream>
#include <fstream>
#include <string>
int main ()
{
string line;
ifstream in("transit_test.py"); //open file handler
if(in.is_open()) //check if file open
{
while (!in.eof() ) //until the end of file
{
getline(in,line); //read each line
// do something with the line
}
in.close(); //close file handler
}
else
{
cout << "Can not open file" << endl;
}
return 0;
}