I am completely new to the forum and this is my fist post so I apologize in advance for inappropriate words. I am not a programmer but I have to use c++ because it is amazingly fast. My question is about file loading and saving. In the script below I want to load in a matrix from a ascii txt file and manipulate it then write the new matrix to ascii (or binary but first the ascii should work). Of course I did not want to reinvent the wheel so the code below is from the Internet and was put together for this purpose. If I change the inFile to matrixin.txt and the outFile to matrixout.txt the code works charmingly and the result is also good. So far so good. But when I change back to the command-line read in version the script still can be compiled but I get an error when running the executable. The error is: Segmentation fault! Can anybody see where I did the mistake?
Do you have a file named "inFile" in your current working directory? You're never using your variable inFile, you use the string constant "inFile" instead. Notice the quotation marks?
You should always check to insure your file properly open properly before trying to read from that file.
thanks for the answers. to Jim: I checked the file and it is in the directory so that should be no problem. If I print the string constant to the terminal it gives me the right file name but then if I put it into the brackets as string it somehow doesnt do the job!
to Cire: I thought one can have variable definitions "on the fly" in C++. I guess then I have to declare the variables in the beginning and choose a variable size vector. Or What is the best solution for definition of a matrix(array) which could change size?
thanks for the answers. to Jim: I checked the file and it is in the directory so that should be no problem. If I print the string constant to the terminal it gives me the right file name but then if I put it into the brackets as string it somehow doesnt do the job!
Read my last post again, carefully. You have a file named inFile in your current working directory?
What is the best solution for definition of a matrix(array) which could change size?
I read your post again and it turns out i didnt reply correctly. I am sorry! You are correct there is no such a file in the directory as inFile. InFile is the second argv[1] read from the commandline:
./trans matrixin.txt matrixout.txt.
So there is a file called matrixin.txt in the directory! So in then matrixin.txt=inFile!
fstream (C++98) does not take a std::string as a paramter (in the next standard C++11 it will) hence you currently need c_str() to provide the c-string equivalent