ifstream - Reading from text file is not working C++ -
i want read text file , don't know why code not working. have put correct text file name on folder program running. must doing small. please highlight issue in code below:
// consoleapplication1.cpp : defines entry point console application. // #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include<cstdio> using namespace std; #ifdef win32 #include <direct.h> #define getcurrentdir _getcwd #else #include <unistd.h> #define getcurrentdir getcwd #endif std::string get_working_path() { char cwd[1024]; if (getcurrentdir(cwd, sizeof(cwd)) != null) return std::string(cwd); else return std::string(""); } int main() { string line; //ofstream myfile; //myfile.open("cmesymbols.txt", ios::out | ios::app | ios::binary); ifstream myfile("cmd.txt"); if (myfile.is_open()) { while (getline(myfile, line)) { cout << line << '\n'; } myfile.close(); } else std::cout << "file not found in cwd: " << get_working_path(); myfile.close(); return 0; }
output: file not found in cwd:
ifstream myfile("cmd.txt");
doesn't create file you.
make sure file "cmd.txt" exists in project directory main.cpp
(or main source file).
Comments
Post a Comment