I'm trying to use "scanf" to read a string line: "is it not working", but I don't know if it's even possible to implement it in this particular example.
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
using namespace std;
int main() {
int i = 4;
double d = 4.0;
string s = "Just an example of, why ";
int number;
double doub;
string longText;
scanf("%d %lf %s", &number, &doub, &longText); //Read a line ex ("is it not working")
printf("%d\n%lf\n%s", number+i, doub+d,s+longText); //Print all the values, but not printing s+longText
}
Image showing the code
%s in printf/scanf family stands for char array, not std::string. If you want to get line, use std::getline. If you want to use std::string buffer for stdio functions, use std::string::data function, but I wouldn't suggest that as buffer-overflow is likely, especially for something like get-line.
Related
I have this program and I want to fill the tables array with the values passed from the command line in integer form . However It string s is only being assigned argument 6 .. what is the problem ?
#include <iostream>
#include <cctype>
#include <locale>
#include <cstdlib>
#include <sstream>
#include <string>
using namespace std;
int main(int argc,char *argv[]){
int i;
int tables[100];
stringstream str;
string s;
int result;
char value;
if(argc <=1){
cout<<"NO ARGUMENTS PASSED"<<endl;
exit(0);
}
/*char value = *argv[1];
cout<<value<<endl;
str << value;
str >> s;
result = stoi(s,nullptr,10);
cout<<result<<endl;*/
for (i=1;i<argc;i++){
if(isdigit(*argv[i])){
value = *argv[i];
str<<value;
str>>s;
cout<<s<<endl;
tables[i-1] = stoi(s,nullptr,10);
}
}
}
isdigit function test if a char is a digit, so the command line
isdigit(*argv[i])
Return true is the firts character of the char* is a digit. What you want is to convert a char* to an integer, I suggest to take a look at the atoi function.
However, the string convertion for printing your result is not necessary.
The problem is that you are using stringstream in the wrong way.
By writing str >> s you are reaching eof in the stream.
To fix this, you can avoid to use stringstream and instead directly assign value to s.
If you want to use stringstream, you can reset it back to initial state after writing to s as follows:
str.str(std::string{});
str.clear();
and use it again
I have an input file which I'm reading in with the basic myFile >> variable since I know the format and the format will always be correct. The file I'm reading in is formatted as instruction <num> <num> and to make >> work, I'm reading everything in as a string. If I have 3 variables, one to take in each piece of the line, how can I then turn string <1> (for example) into int 1? I know the string's first and last characters are brackets which need to be removed, then I could cast to an int, but I'm new to C++ and would like some insight on the best method of doing this (finding and removing the <>, then casting to int)
use stringstream
#include <string>
#include <sstream>
#include <iostream>
int main() {
std::string str = "<1>";
int value;
std::stringstream ss(str);
char c;
ss >> c >> value >> c;
std::cout << value;
}
First to get the middle character out you can just do char myChar = inputString.at(1);. Then you can do int myInt = (int)myChar;
Even if you remove the <> characters, your still importing the file content into a string using >> so you still need to cast it to an int. If you have only 1 value, you can follow what Nicholas Callahan wrote in the previous answer, but if you have multiple characters you want to read as int, you dont have a choice but to cast.
You can also resort to sscanf.
#include <cstdio>
#include <iostream>
#include <string>
int main()
{
std::string str = "<1234>";
int value;
sscanf(str.c_str(), "<%d>", &value);
std::cout << value << std::endl;
}
This is for a project and this is only a single piece of the entire program that I'm hung up on.
I am being given a file where the format of the information is:
200707211245 F70.5
The numbers before the space are the YYYYMMDDTTTT T=time and I have to output to a new file in the format:
21.38 C --- recorded on 07/21/2007 at 12:45
This is fairly straight forward process, but I can't figure out how to change the first 7 numbers into an integer that I can pass to a function to format the date and time correctly. I am using Visual Studio 2013.
Subsequently this is all I've been able to do. Any and all help would be greatly appreciated. I'm getting an error over the .c_str() portion of the code.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ifstream inFile;
inFile.open("inputData.txt", ios::in);
ofstream oFile;
oFile.open("results.dat", ios::out);
int date = atoi(inFile.c_str());
return 0;
}
int getDate(int date)
{
}
First some notes:
atoi return and int value, your number 200707211245 have 12 digits (int range is –2,147,483,648 to 2,147,483,647, ten digits max), your number don't fit in the type range, you would obtain strange results.
Better to use lexical_cast from boost
Here is an example:
#include <iostream>
#include <fstream>
#include <boost/lexical_cast.hpp>
using namespace std;
int get_date(long long date) {
std::cout << date << std::endl;
return date;
}
int main()
{
ifstream in_file("H:\\save.txt", ios::in);
if (!in_file.is_open())
return -1;
std::string date_str;
in_file >> date_str;
int date1 = atoi(date_str.c_str()); // would overflow
long long date2 = boost::lexical_cast<long long>(date_str);
get_date(date2);
return 0;
}
Expected output:
200707211245
The simplest way to solve this problem is to parse the string input into separate strings for Y, M, D, H, M and temp. Read a line of input and divide it into pieces. You probably don't want to use Boost for a class. Just use std::string and the string operations in the standard library.
Then you can convert those little strings into little integers if you like, or just reformat them blindly into the output.
The temperature string will have to be converted to double in order to convert to Celsius.
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
char x[20];
cout << "enter something\n";
cin.getline(x,20);
ofstream o("d:/tester.txt");
//o.write( (char*)&x , sizeof(x) );
for(int i = 0 ; i<=19 ; i++ ) {
o.put(x[i]);
}
}
I am not getting that output in the file the one which i enter during program . for eg. the output is 畳慨汩朠灵慴찀쳌쳌쳌 on writing suhail gupta.
What is the problem with the code ? Even when i use o.write( (char*)&x , sizeof(x) ); (the commented statement) i get the same output.
What is the reason?
Your program involves undefined behavior. The x array is not fully initialized and you read from the uninitialized indices. Besides, you always write 20 bytes, independent of what you read from the user.
I guess you use some text editor like Notepad. The latter has bugs when trying to guess the encoding. It appears that it guesses the file is UTF16 and displays 20/2 == 10 characters instead.
To solve the problem, store to the file exactly the number of characters entered by the user. Use std::string to make it easier.
Edit: The C++ way:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
string x;
cout << "enter something\n";
getline(cin, x);
ofstream o("d:/tester.txt");
o << x;
}
I'm trying to print a string the following way:
int main(){
string s("bla");
printf("%s \n", s);
.......
}
but all I get is this random gibberish.
Can you please explain why?
Because %s indicates a char*, not a std::string. Use s.c_str() or better still use, iostreams:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("bla");
std::cout << s << "\n";
}
You need to use c_str to get c-string equivalent to the string content as printf does not know how to print a string object.
string s("bla");
printf("%s \n", s.c_str());
Instead you can just do:
string s("bla");
std::cout<<s;
I've managed to print the string using "cout" when I switched from :
#include <string.h>
to
#include <string>
I wish I would understand why it matters...
Why don't you just use
char s[]="bla";