I am setting up my environment to run a C++ program on VSCode in Windows 10. I have MINGW installed and the path (C:\MinGW\bin) added to my environment variables. I have added the appropriate extensions in VSCode.
In my hello world program. It works just fine. However, when I open a file, nothing prints from anywhere in the entire program.
I am compiling like this:
g++ main.cpp -o main
And running the program like this:
./main test.txt
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[]) {
cout <<"Here\n";
ifstream file; //This is the line that causes no output
file.open(argv[1]);
return 0;
}
The file is not the problem. I have tried multiple file types like .txt and .csv.
Related
I write small test application in C++ to read the file data, So when I used the C++ -Stream class to read from files, it fails to open the file stream for the file name which gets passed as argument of the Stream class object.
Also I keep my file(Test.txt) in the directory where my program executable is kept and run.
#include<fstream>
#include<string>
#include<iostream>
using namespace std;
int main(int argc, char *argv[])
{
ifstream testFile("Test.txt");
string line;
if (testFile.is_open())
{
while (getline(testFile, line))
{
line += line;
}
}
}
please also find the additional debug capture which has been captured from visual studio debugger.
If you run a program from the editor/IDE it is not always true (and generally it isn't) that the root directory for relative paths is the project directory or the one where the .exe is placed. If you run the executable from the command line placed in the directory it should work, but if you run it when the command line isn't in the directory you'll see the same error.
If you want the root directory to be the project one there is certainly an option in the editor/IDE, just surf the web or look into settings (probably run configurations)
The answer is: The file could not be opened. It is mispelled, or not existing.
But most likely it is in a different directory.
Please use a full path for the file, then you can open it. Like
ifstream testFile("c:\\temp\\Test.txt");
Then, please do not use is_open.
Simply use
if (testFile)
The ! operator for ifstream is overwritten, so this will work.
thanks all and I manage to get it working with the platform independent method. Please see the code below.
#include<fstream>
#include<string>
#include<stdlib.h>
#include<iostream>
#include <errno.h>
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
int main(int argc, char *argv[])
{
fs::path Path = fs::current_path() / argv[1];
ifstream testFile(Path.c_str());
string line;
string str;
if (testFile.is_open())
{
for (std::string line; std::getline(testFile, line);)
{
str += line;
}
}
std::cout << str << endl;
}
I have been working on C++ for few years now and have compiled stuff several times but the following issue is totally new to me and it just doesn't make sense.
Following are the steps i am following:
using cygwin setup with g++ version:6.4.0 and clang++ version:4.0.1
created a new cpp fie using sublime text added simple cout and compiled
with the command: clang++ -g -Wall -std=c++14 thread1.cpp -o thread, works fine.
added new contents maybe another cout, this time upon compilation i
get a ton of errors stating its not utf-8 file.
saved the file using utf-8 encoding in sublime text and also tried
with utf-8 BOM encoding, still getting same not utf-8 file error.
ran the file command in cygwin to check file encoding , file -i
thread1.cpp, got output as thread1.cpp: text/x-c; charset=utf-8.
Any pointers to what might be going wrong here?
Following is the code which compiles:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello";
return 0;
}
Following code gives error:
#include "iostream"
#include "thread"
#include "mutex"
using namespace std;
class threadFunctor{
public:
};
int main(int argc , char** argv){
cout << "Hello World";
return 0;
}
Following is a snippet of the errors generated:
./thread:3:29: error: source file is not valid UTF-8
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:3:30: warning: null character ignored [-Wnull-character]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:3:31: warning: missing terminating ' character [-Winvalid-pp-token]
$<U+0000><U+0000><U+0000><U+0000><U+0000><U+0000><U+0000>PE<U+0000><U+0000>d<86><U+0014><U+0000><87><F5><CC>Y<U+0000><U+0014><U+0001><U+0000><A9><U+0002><U+0000><U+0000><F0><U+0000>'...
^
./thread:4:3: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
^
./thread:4:4: warning: null character ignored [-Wnull-character]
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>...
^
./thread:4:5: error: source file is not valid UTF-8
5<U+0001><U+0000><U+0000><E8><B0><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C4><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><C8><U+0001><U+0000><U+0000>E1<C0>1<D2>1<C9><E8><CC>.
By doing #include "thread", you've said (by convention at least) to check the current directory for a thread file first. The problem is that you have a thread file in your current directory. Presumably, it's your executable from the first time you compiled. This is apparent by the errors, which explicitly say ./thread.
You should include all standard headers via #include <thread> rather than #include "thread" - you never want to search elsewhere first for standard headers.
Hi I am new to C++ and Code::Block
I am trying to make a simple code to test it, using strings.
when I compile the code there is no problem, but when I try to debug it, Code::Block gives me the following warning:
Cannot open file:
File:../../../../../src/gcc-4.9.2/libgcc/unwind-sjlj.c
Info: "Multiple information windows with the same message have been
supressed."
Image of the error FYI:
Part of the code that gives me an error.
inside main function
#include <iostream>
#include <string>
int main ()
{
std::mystring("What's wrong with strings");
return 0;
}
I realise that this error only occurs when I try to debug a string or a file containing a string.
Any help would be appreciated.
some other information that might help:
Code::Block 16.01
Compiler MinGW gcc4.9.2
Windows 7 Professional 32 bits SP1
First of all, to use strings you must include the file header string. And the name of the type string is..std::string, not std::mystring.
#include <string>
int main(int argc, char** argv)
{
std::string mystring("Nothing's wrong with strings");
return 0;
}
#include <iostream>
using namespace std;
int main ()
{
string mystring = "Whats wrong with my string";
return 0;
}
If you write it in the following way, it should work.
It's safer to define strings like I showed it. It will be also easier for you if you add using namespace std in the beginning of every program if you are new to C++.
This question already has answers here:
main.cc:5:30: fatal error: folder/file.h: No such file or directory
(3 answers)
Closed 9 months ago.
I began learning MinGW to compile C++ program. I had a sample C++ file including test.cpp (main program) and srfft.h (extra header file I added, not from the libray). The process executed as below step:
g++ test.cpp -o test.exe
test.exe
#include <iostream>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
I got the correct answer, but when I added #include in the C++ code as below:
#include <iostream>
#include <srfft.h>
using namespace std;
int main()
{
cout<< "Hello World!\n";
return 0;
}
CMD showed me" fatal error: srfft.h:No such file or directory"
How do I execute my code with MinGW?
Where is the problem?
Try
#include "srfft.h"
Notice "" instead of < and >. With "srfft.h", the file is searched relative to the current directory, whereas with <srfft.h> the search only takes place in the system and specified include directories, which normally doesn't include the current directory.
I'm completely new to C++, but I have created a minor program, looking to port the program to other computers, but when I "install" the program I get this error...-static-libgcc -static-libstdc++ missing, is there a file I should be including in the program itself, or is this a library I have to install on each computer? The computers that I expect to run the program will be windows xp. Source code of the file is as follows:
#include <stdlib.h>
#include <windows.h>
#include <direct.h>
#include <string.h>
#include <string>
#include <iostream>
using namespace std;
int main(int argc, const char *argv[])
{
_chdir("C:\\Program Files\\NCHSoftware\\Talk\\");
string number = "start talk.exe -dial " + std::string(argv[1]+4);
system(number.c_str());
exit;
return 0;
}
They are shared lib's that would need to be on the host computer.
To learn how to compile a static version;
See here: http://gcc.gnu.org/onlinedocs/gcc/Link-Options.html
Read the "-static-libgcc" & "-static-libstdc++" sections.