C++, Same code won't work btw CLion & Visual Studio - c++

I have the following code in VS:
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inData;
int temp;
inData.open("test_file.txt");
do {
inData >> temp;
cout << temp << " ";
} while (!(inData.eof()));
inData.close();
return 0;
}
test_file.txt contains the following data (it's a single line):
1 2 3 4 5 6 7 8 9 0 10 11
When I built and ran this in VS, it worked fine. Today, when I tried to run it on CLion, it gives me garbage data. I tried creating a new project from scratch in CLion and copy-pasting just the code in main.cpp, but still gives me random data. And now I'm wasting my Saturday wrestling with my IDE instead of just finishing my homework. Any help?

I figured out what the issue is. CLion doesn't recognize the parameter as a path, whereas VS does.

Related

How to use file strings as commands in c++

I am trying to make a program on Windows 10 using Visual Studio 2015 that would sim-link certain files to certain locations. I am trying to make a text file with the location of the files, and the sim-link destination to use.
This is an example of the file data that would be in the properties.txt file:
FileLocation: "Z:\Folder\file.txt"
FileMkdirLocation: "Z:\Folder2\file.txt"
I want to use something like system(mkdir "sim-link_file_location" "file_location") by changing the data that is in properties.txt. I want to be able to add more than 1 file, without recompiling the program and writing each command for each file, one by one.
The problem is that I don't know how to make the commands use the data in the file.
EDIT: I managed to find out a way, but I get errors when compiling the program. I use this code:
#include <iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>
using namespace std;
//initialization of Properties File used
ifstream PropertiesFile ("PropertiesFile.txt");
int main()
{
//initialization of variables used
int input_option;
char FileLocation[256], Command[]="mklink ";
// string FileLocation, Command;
PropertiesFile >> FileLocation;
/* switch (input_option)
{
case "add all mods":
}
*/
cout << "FileLocation: " << FileLocation;
cout << endl;
strcat(Command, FileLocation);
Command[strlen(FileLocation)] = '\0';
cout << Command;
cout << endl;
//system(command);
system("pause");
return 0;
}
I know that i haven't used all variables yet.
It tells me that "strcat" is deprecated and to use "strcat_s" instead, and when i replace it with that, I get
"Debug Assertion Failed - Expression: (L"Buffer is too small" && 0)"
I had to make the "Command" char bigger than "FileLocation" because then strcat_s would not be able to copy the content. After that the program worked fine, and there were no other Assert Errors.
The command to create a soft link in linux is: ln -s <source> <destination>
You can use this in a system(""); call, BUT before you continue in your code, you will have to make sure that the kernel finished executing this command.
After that you can read the link as if it was the original file.

Why does MinGW and GCC give me different outputs for this simple program?

In a freshman CSc college course with C++, we had a problem to convert a decimal number to a binary value.
My code for this is:
#include <iostream>
#include <math.h>
using namespace std;
int main()
{
int x, bin=0, i=0;
cin>>x;
while(x!=0)
{
bin+=(x%2)*pow(10,i++);
x/=2;
}
cout<<bin;
return 0;
}
When I built it on Kubuntu 15.10 using gcc 5.2.1 with no extra build flags, I get the expected output of 10100 for an input of 20.
Yet, when I build it when MinGW packaged with CodeBlocks 13.12 on Windows 10, I get the output of 10098 for the same input of 20.
This bizarre deviation from the right answer increases with the number of places for the final output in MinGW. What could be going wrong and why is the MinGW output give me the wrong answer?

How to output my data into a new csv file each time I start my C++ program?

Im a about to develop a program that will prompt user to input numbers and the program will make several calculation and will store the output datas into a csv file.
My question is :
How can I copy and store the data into a new .csv file instead of the existing one each and every time after:
1) I quit and restart the program
2) When the program suddenly terminates while running(example:My computer suddenly turns off while im running the program due to external errors)?
P.S:Please solve or explain to me with sample codes instead of just words cz im new to c++.
Thanks in advance :)
#include <iostream>
#include <fstream>
#include<string>
#include <iomanip>
using namespace std;
int main()
{
float a,b,c;
ifstream indata;
ofstream outdata;
outdata.open("Out.csv", ios::app);//opens the csv file
outdata << "Num1,Num2,Answer" << endl;//'prompt user for numbers
while (!(a ==-100))//calculate the product of the numbers
{
cout<<"Enter Num1:";
cin>>a;
if(a==-100)break;
cout<<"Enter Num2:";
cin>>b;
c=a*b;
outdata<<a<<","<<b<<","<<c<< endl;
}
indata.open("out.csv");//stores data into the csv file
system("pause");
return 0;
}
1) Assemble the filename from the current date and time.
2) If the hardware suddenly stops working or the process is forcibly terminated with kill -9 or Task manager, you get no notification and so you cannot reliably achieve what you seem to want.
'Please solve or explain to me with sample codes instead of just words cz im new to c++'. Get better by writing code and getting it to work:)
#include <time.h>
time_t now;
time(&now);
char outputFileName[128];
sprintf(outputFileName,"%ld_my_data.csv",now);

New to MacOS and Xcode; Strange errors in Xcode while running. Worked just fine in Eclipse

UPDATE: I could solve the problem. Question can be closed! (If you wonder how: check solution)
I got a little problem. I recently decided to buy a Macbook Air since we have to work with POSIX based functions such as pthread_create, fork etc (parallel programming). It's my first laptop with MacOS on it.
I installed Xcode on it and started a new c++ project. I then wrote my program and tried to run it. There were no errors and the build succeeded but when i tried to run it i got SIGABRT errors at the strangest places. I didn't know why that happened so i copied the code in an eclipse project on my windows desktop and it worked just fine. Can you help me with this? What could be the issue? Did I maybe forget to install something?
#include <iostream>
#include <unistd.h>
#include <fstream>
#include <vector>
#include <map>
#include <iterator>
#include <cstdlib>
int main(int argc, const char * argv[])
{
const char* sequenceFile=argv[1];
const char* patternFile=argv[2];
std::ifstream file;
file.open(sequenceFile,std::ifstream::in);
std::vector<std::string> k;
std::string line;
if(file.is_open()){
while(std::getline(file,line)){
k.push_back(line);
}
}
int lengseq=atoi(k.at(0).c_str()); //for example SIGABRT here
std::string sequence = k.at(1);
file.close();
file.open(patternFile,std::ifstream::in);
k.clear();
if(file.is_open()){
while(std::getline(file,line)){
k.push_back(line);
}
}
std::map<std::string,int> patterns;
for(unsigned i=1;i<k.size()-1;){
patterns[k.at(i+1)]=atoi(k.at(i).c_str());
i+=2;
}
//serial
bool seqCheck = false;
typedef std::vector<std::pair<std::string, int> > my_vector;
my_vector patternOccurences;
typedef std::map<std::string,int>::iterator it_type;
for(it_type iterator=patterns.begin();iterator!=patterns.end();iterator++){
for(int i = 1;i<lengseq;i++){
if(i+iterator->second-1<=lengseq){
for(int j=0;j<iterator->second;j++){
if(sequence.at(j+i-1)==iterator->first.at(j)){
seqCheck=true;
}
else{
seqCheck=false;
break;
}
}
if(seqCheck==true){
patternOccurences.push_back(std::make_pair(iterator->first,i));
}
}
}
}
for(unsigned i =0;i<patternOccurences.size();i++){
std::cout<<"Pattern: "<<patternOccurences.at(i).first<<" at index "<<patternOccurences.at(i).second-1<<std::endl;
}
return 0;
}
That was the code i was using. I'm sorry for the mess in the code. And in case you're wondering I did indeed set the command line arguments so that's not the issue.
EDIT: It seems like the files the files cant be opened. I added:
else{
std::cerr<<"Couldn't open file!";
return 0;
}
after if(file.is_open()) and it jumps straight in there. I set the filename (command line arguments with project->scheme->edit scheme and then added as arguments and i have the textfiles in the project directory. What could be the issue?
SOLUTION: The problem was that i had to type the entire path to the textfiles. I was used to have the textfiles in the project directory and then i didnt have to type the path while i was working with eclipse on my windows desktop (probably because of the workspace). Thanks to all who helped. Ill remember to type the path from now on when I'm working with Xcode. ;)
Thank you in advance and I hope you can help me.
Kazoooie

How do I run my code from the command line?

i have following code
#include <iostream>
using namespace std;
int main(int argc,char arg[]){
int a=arg[1];
int b=arg[2];
int c=a+b;
cout<<c<<endl;
return 0;
}
i am using windows 7 microsoft visual c++ 2010
how run it from command line?
Open a command prompt from the Start Menu. Use the CD command to change directories to where your exe is. type the name of your exe followed by the arguments.
foo.exe 1 2
or just
foo 1 2
Expect the output (once you've fixed your numerous code errors):
3
Once you compile this you get an executable. Navigate to the directory containing the executable and run it.
Go to google and look for a windows console tutorial. You need to start it from the console. Alternatively you can assign command line in the project properties. I'd recommend learning to do both.
BTW, this code almost certainly does not do what you think it does.
The compiled output of your program will be in the Debug or Release folder inside the solution folder (at least with default project settings). Just change to that directory and run the .exe file.
Open the Visual Studio Command Prompt (you can find it in the Start Menu)
cd to your source file directory
type:
cl.exe <your file name>.cpp
It will create a file .exe
Once your code is setup properly it would be something like this.
MyApp 2 3
Or similar
Navigate to the directory where the executable (.exe) is located. Then type the executable's name followed by two integer parameters.
C:\TestProg\> TestProg 5 6
The problems in your original example are corrected here:
#include <iostream>
#include <sstream>
int main(int argc, char *arg[])
{
std::stringstream sa;
std::stringstream sb;
int a;
int b;
int c;
if (argc >= 3)
{
// Convert string parameter into an integer.
sa.str(arg[1]);
sa >> a;
if (!sa)
{
return 1; // error
}
// Convert string parameter into an integer.
sb.str(arg[2]);
sb >> b;
if (!sb)
{
return 1; // error
}
}
else
{
return 1; // error
}
c = a + b;
std::cout << c << std::endl;
return 0;
}