C++ :: Using Fgets To Read In A String - No Conversion Function Error
May 27, 2013I am trying to use fgets to read in a string, but keep getting a "no conversion function from std::string to char" error.
View 2 RepliesI am trying to use fgets to read in a string, but keep getting a "no conversion function from std::string to char" error.
View 2 Replieschar intToStr(int a) {
int n, i, j, sign, set;
char r[10], s[10];
if (a[0] == '-')
sign = -1;
if (sign == -1)
[Code] ....
I have doubt at the time of handling of negative numbers at the time of converting to string ....
I have prepared a file through the use of following code
Code:
fprintf(file2, "%i %i %i %i %i %i
",
msg_id,
msg_size,
msg_period,
msg_deadline,
msg_producer,
msg_comsumer
);
As one can see, this file has tab separated integer entries. The file is written correctly, let us call this file "msg.txt".
I face the problem when I read this file, using fgets as follows:
Code:
char singleMessage[100];
while( fgets(singleMessage, sizeof(singleMessage), file ) )
{
puts(singleMessage);
sscanf(singleMessage, "%i %i %i %i %i %i
",
&first, &second, &third, &fourth, &fifth, &sixth);
fprintf(stderr, "first: %d, second: %d, third: %d, fourth: %d, fifth: %d, sixth: %d
",
first, second, third, fourth, fifth, sixth);
}
but fgets only retrieves until the first, i.e, if the first line in the file reads:
788004425
fgets returns only 78.
Does it have to do with how the file was written in the first place.
I'm new to C/C++. I am attemping to use fgets and sscanf to read a line of input, and confirm it is a positive number.My code works great, except for the case of a negative number. When I enter a negative number, my while loop seems to run infinitely, with stdin providng the same input over and over again.
Here's the code snippet:
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define TEXT_LEN 64
void foo() {
char* inStr = (char*)malloc(sizeof(char)*TEXT_LEN);
memset(inStr, 0, TEXT_LEN);
}
[code]....
called object 'fptr_in' is not a function or function pointer
called object 'fptr_out' is not a function or function pointer
what can i do for the errors?(i mustn't use loop and arrays for the code)
Code:
#include <stdio.h>
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out);
int main()
return 0;
}
void sum_of_2nd_and_3nd( FILE* fptr_in, FILE* fptr_out){
}
[code].....
How can i play a mp3 file in the background?How can i rule the volume of the mp3 file?why does delay(0.25) doesnt work in this code?
Code:
FILE *datei;
char line[ANZAHL][LAENGE];
int i ;
datei = fopen ("song.txt", "r");
if (datei != NULL){
for(i = 0; i < ANZAHL;i++) {
delay(0.25);
fgets(line,LAENGE,datei);
printf ("%s
[code]....
I'm using fgets which will read a single line at a time, but unlike fgets I don't want it to return the new line char ( ) ?I want to be able to use printf to display the lines next to each other.
View 7 Replies View RelatedIn the assignment we are forbidden to use fscanf(). I have been trying to get this to work, but I've started to realize that I do not have a complete understanding of what strtok() actually does. I'm getting this warning when debugging: "assignment makes integer from pointer without cast."
This warning happens when assigning str to goal and assist, and I think it is because they are, when dereferenced, integers. The code below correctly assigns the name into the correct spot, but leaves nonsense data in the goal and assist arrays.
ex:-7880, -7888 file example: NAME GOALS ASSISTS JOHN 1 2
Code:
void readLinesFromFile( FILE* Ptr, int* goal, int* assist, char** name, int lines ){/*
* Reads lines from files and populates the arrays with the corresponding info.
*/
int index;
char hold[ MAX_LINE ] = { 0 };
char* str = NULL;
[Code] .....
From what I understand about strtok(), it returns a string, and takes in a character array and a key value that tells it when to stop. In the online examples I've seen, they use NULL in the first field. I'm not sure why.
The compiler keeps on telling me that invalid conversion from wxBitmap* to wxString on the line with the AddTool function, whiles I do not even try to do such an ambiguous typecast.
wxWidgets 2.9.4
MinGW
gdb
Code:
#include "mainwnd.h"
//namespaces
//other definitions and declarations
CMAINWND::CMAINWND(const wxString& szTitle):wxFrame(NULL,wxID_ANY,szTitle) {
wxImage::AddHandler(new wxBMPHandler);
[Code] .....
So I get errors like:
Error1error C2440: 'initializing' : cannot convert from 'const char [4]' to 'Course'
Error2error C2440: 'initializing' : cannot convert from 'int' to 'Course'158
Error3error C2440: 'initializing' : cannot convert from 'const char [6]' to 'Course'158
Error4error C2078: too many initializers158
# include <iostream>
# include <cstring>
#include <iomanip>
#include <cmath>
using namespace std;
class Course {
public:
char CourseName[10]; // Array of size 10, 9 characters and 1 null terminator
[Code] .....
And in the /// part I also need to use the dot operator and the arrow operator to print on the screen info about the second and third Courses.
Write your question here. Hello World not working
they say
1>------ Build started: Project: HelloWorld1, Configuration: Debug Win32 ------
1>LINK : fatal error LNK1123: failure during conversion to COFF: file invalid or corrupt
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I used this video [URL] ....
#include <iostream>
using namespace std;
int main(){
cout <<"Hello World! " << endl;
return 0;
}
I try to use passing function as argument but I'm stuck. I have two questions: First, I try to call uppercase and open .txt in tfm Second, How can I read characters from in.txt as string and assign to char content[] ?
#include <stdio.h>
void tfm( char str_filename[], void(*pf_convertion)( char content[]));
void uppercase(char content[]); //converts all letters to uppercase
int main(){
puts("-------------------------------");
printf("tfm:
");
tfm("in.txt", uppercase);
[Code] ....
I made a simple binary tree then decide to try out threads too. I got the following error:
call of an object of a class type without appropriate operator or conversion
Code:
#include "Tree.h"
#include <iostream>
#include <thread>
void main(void){
[Code] ....
I am having a hard time figuring out why the error exists. I tried adding the new operator but that did not work.
I'm writing a small pice of code that takes numbers and convert it to string. So that's what I got
int main() {
char letter;
char letter1;
char letter2;
cin >> letter;
cin >>letter1;
int sum = letter * 128 + letter1;
[Code] ....
Then it asks the user to enter a letter, say 'w' , which it's value is 119
Then enter another letter, say 'o' which value is 111
then it do the equation, which is take the value of the first letter multiple it by 128 then add it to the value of the second letter.
How can I make it does this process in a For loop, or any kind of loop??
Then I have the second part which is the other way around. That is when the user enter a numbers then the code convert these numbers to a string.
251394404 - "d"
1964018 - "rd"
15343 - "ord"
119 - "word"
so what's happening here is the it divide each time by 128. But I can't do that in a code.
I have a question on conversion between char & string. I have cut & pasted the part of the code from my C++ code and my function "decryptPwd" uses C style code with "char" etc.
I need to pass a string (mypwd) somehow to this function after conversion & then compare it to another string (newmypwd).
I tried - "decryptPwd(mypwd.c_str())==newmypwd.c_str()" but it did not work.
..
#include <string>
..
char* decryptPwd(char hash[64]);
main () {
string mypwd;
string newmypwd;
if (decryptPwd(mypwd)==newmypwd)
[Code] ...
I want a code that can convert floating/double value into string/char array(char arr[]) and also it can be run on Boreland C++ 5.02
Here I've a code but it doesn't show all the numbers. Instead, it's showing in exponential form which I don't want!!
int main() {
char* str = new char[30];
float flt = 2.4567F;
sprintf(str, "%.4g", flt );
cout<<str<<endl; //Exponential form even after 6 digits without decimal
return 0;
}
How to convert int into string ? I had done conversion string to int.
My code :
/*Convert string into int using stringstream */
#include <iostream>
#include <string>
#include "console.h"
#include <sstream>
using namespace std;
[Code] .....
I have the following piece of code:
string ss = findNodeValue( str, "Horizon");
cout << "ss is: " << ss << endl;
double dd = atof( ss.c_str());
cout << "dd is: " << dd << endl;
When the value of 'ss' is printed, I find it prints 1.0, but when the value of 'dd' is printed, it prints 1 whereas it is supposed to print 1.0.
I have a problem in using ATL 7.0 string conversion macros.
My codes looks like this, which uses ATL 3.0 string conversion macros in the past:
Void Myfunc()
{
USES_CONVERSION;
LPSTR lpszA;
LPWSTR lpszW;
If (...) {
CString strText;
If (...) {
If (bUnicode)
[Code]...
But since 3.0 macros do not support large strings, I want to switch to 7.0 macros, but have problems. Based on the [URL]... samples, I should declare CT2A pszA(strText) or CT2W pszW(strText) within the if and else bodies, as below:
Void Myfunc()
{
USES_CONVERSION;
LPSTR lpszA;
LPWSTR lpszW;
If (...) {
[Code]...
However, in such a case, after running to the codes using lpszA or lpszW, both CT2A and CT2W will be destructed so lpszA and lpszW will be invalid. How to solve this problem?
I have this old c function that takes as an argument a char*. but my app is written in cpp so i used std::string to store my strings. to pass a char * to the function i tried :
Code:
char *input = new char[args.i.length() + 1];
strcpy(input, args.i.c_str()); and then Code: function (input);
and
function ( (char *)input);
But I still get this warning message which i would like to fix:
Warning: deprecated conversion from string constant to "char*" [-Wwrite-strings]
following code that I'm reading out of the book "The C++ Standard Library".
class C
{
public:
explicit C(const std::string & s); // explicit(!) type conversion from strings.
...
[Code].....
Now I understand that they are saying that an explicit conversion isn't allowed but what I don't understand is what explicit conversion would be happening if there was one allowed.
Specifically I don't understand the const C & elem syntax. How does this work since the collection holds strings. What would be the syntax for how this:
const C & elem
gets strings. I was thinking it was a class reference that someone how converts to a constructor function pointer or something but i'm really confused.
I wrote the following code but i got this error: Deprecated conversion from string constant to 'char*':
#include <iostream>
using namespace std;
#include <stdio.h>
#include <windows.h>
#include <winuser.h>
int Save (int key_stroke, char *file);
void Stealth();
[Code] ....
I have 1 struture:
Code:
struct SetText{
int PosX;
int PosY;
char *Text;
};
And heres how i add the values:
Code:
SetText *x=new SetText;x->PosX=5;
x->PosX=6;
x->Text ="hello mother";
why i receive that warning in: x->Text ="hello mother"; ?
"deprecated conversion from string constant to 'char*' [-Wwrite-strings]"
So, I'm supposed to do : Create a function with unlimited number of arguments, which forms a dynamic string based on the following form (%d, %s, %f, %lf, %c), with the following prototype:
char*create(char*form, ...);
The function is supposed to have the following output:
create("Peter is %d years old and is in %s-%c class.",7,"second",'A');
-> Peter is 7 years old and is in 7-A class.
create("His GPA is %lf.",4.96);
-> His GPA is 4.96.
create("His favourite subject is math!");
-> His favourite subject is math!
I've managed to do the following :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
char *create(char *form, ...) {
char *res =(char*)calloc(1,1),*pos_int,*pos_float,*pos_str,pos_char,*pos_long;
[Code] ....
The part with %d and %s string was not that hard, but now I'm supposed to convert %f and %lf to string, I've tried using sprintf but I've had no luck so far, another problem is the fact that I've gotta use lists to complete the task. I've been trying to convert float to string for the past 2 hours, but I'm drawing a blank now.
i have used single argument conversion function which gets called in below scenario.
Code:
#include<iostream>
using namespace std;
class Demo
{
[Code]....
It is giving error "test3.cpp: In function `int main()':test3.cpp:18: syntax error before numeric constant"
But it should work as Demo d=100; works
So I'm writing a data structure from scratch as part of a university assignment in c++, and I have to write an iterator for it. The problem involves comparison between constant iterators and normal iterators, and I'm going about it in this way: I wrote a constructor of normal iterator which takes a const iterator as its only parameter, hoping that the comparison operator between two normal iterators will be enough:
btree_iterator(const_btree_iterator<T>&conv):_target(conv._target),_index(conv._index),_last(conv._last){}
(and somewhere else)
template <typename T>
bool operator!=(btree_iterator<T> one,btree_iterator<T> other){
return !(other == one);
}
and I'm testing it in this way:
btree<int> bl(5);//the data structure
auto iter = bl.begin();
iter != bl.cend(); //trying to compare iterator with const iterator
but apparently this is wrong, since the compiler tells me something along the line of "no function 'operator!=' which takes ......" It seems the constructor is alright, since the following works:
btree<int>::iterator i(bl.cend());
Am I getting something fundamentally wrong about it? How is this functionality actually implemented in C++ library containers like vector?