0

I am simply want to read text from a file and don't know why code is not working. I have already put correct text file name on folder from where program is running. I must be doing something small. Please highlight issue in code below:

// ConsoleApplication1.cpp : Defines the entry point for the 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:

Neeraj Kaushik
  • 354
  • 1
  • 5
  • 20
  • How are you compiling this? If you are using MSVS then typically you need to put the file in the same directory that the source files are in. – NathanOliver May 13 '15 at 17:40
  • I am compiling with MSVS and when I run console exe from command prompt then folder is coming out exe folder. – Neeraj Kaushik May 13 '15 at 17:43
  • The current working directory string is null because it is in your local project folder, therefore it is the default one (and it doesn't need to specify anything special). The point is, that the file is not in your project folder (notice that when using VS, for example, you should put the files you use where your code is, and not where the executable is). – Zach P May 13 '15 at 17:46
  • 1
    Just write a full path to a file. And by the way, aren't you see in the ouput: the function `get_working_path()` didn't return a path. – Hi-Angel May 13 '15 at 17:48
  • I tried with full path like "C:\\cmd.txt" , "c:\cmd.txt" and copied to project folder next to .cpp file. It is really strange why not working. – Neeraj Kaushik May 13 '15 at 17:52
  • If you are running this in VS. You can set up the current working directory in the project options. Set this to the same directory where your file lives. Alternatively explicitly set the working directory. [SetCurrentDirectory](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365530%28v=vs.85%29.aspx) – Martin York May 13 '15 at 20:46

2 Answers2

2

This code is working fine. I found that folder settings in machine is to hide known extensions of files. I deliberately put name as "cmd.txt" however actual name came up as "cmd.txt.txt" and because of this code is not finding this file..

I corrected file name as "cmd.txt" and the code is working now.

JensB
  • 839
  • 4
  • 19
Neeraj Kaushik
  • 354
  • 1
  • 5
  • 20
0

ifstream myfile("cmd.txt"); doesn't create the file for you.
So make sure the file "cmd.txt" exists in your project directory together with your main.cpp
(or main source file).

Andreas DM
  • 10,685
  • 6
  • 35
  • 62