| | |
Adding binary values from a file
![]() |
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Solved Threads: 0
Hello, I'm working o a function that takes a file with binary numbers and returns the sum of those numbers. Here's where I'm now
and here's an error I'm getting when building it:
error C2664: 'strtol' : cannot convert parameter 1 from 'std::string' to 'const char *'
I thought strtol needed a pointer to the string, but changing line to pointer to line didn't help. Anyone can point me in a right direction?
C++ Syntax (Toggle Plain Text)
int Sum (char *fileName) { char * numEnd; long temp(0); int result(0); string line; ifstream file (fileName); if (file.is_open()) { while (! file.eof() ) { getline (file,line); //Convert line to an long and sum up with the rest temp = strtol(line, &numEnd, 2); result = result + temp; //Done } // close the stream file.close(); } return result; }
and here's an error I'm getting when building it:
error C2664: 'strtol' : cannot convert parameter 1 from 'std::string' to 'const char *'
I thought strtol needed a pointer to the string, but changing line to pointer to line didn't help. Anyone can point me in a right direction?
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Solved Threads: 0
OK, I got it. Any comments on code quality or improvements are still welcome!
C++ Syntax (Toggle Plain Text)
int Sum (char *fileName) { char * numEnd; long temp(0); int result(0); string line; ifstream file (fileName); if (file.is_open()) { while (! file.eof() ) { getline (file,line); //Convert line to an int and sum up with the rest char *linePt = &line[0]; temp = strtol(linePt, &numEnd, 2); result = result + temp; //Done } // close the stream file.close(); } return result; }
It's because
Better yet, use the C++ streams, the modern equivalent of a lot of the old C string functions, rather than mixing C and C++.
Hope this helps
strtol was an old C function written far before C++ strings were invented. Although C++ strings have overloaded functions to make transitions from the older-style char strings to C++ strings and vice-versa, this problem can't be as transparently solved when passing a C++ string to a function that is expecting a char string. To remedy this, C++ strings include a nifty member function called c_str() that can quickly return the char string equivalent. Thus, you could code it like this: C++ Syntax (Toggle Plain Text)
temp = strtol(line.c_str(), &numEnd, 2);
Better yet, use the C++ streams, the modern equivalent of a lot of the old C string functions, rather than mixing C and C++.
Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."
![]() |
Similar Threads
- Adding binary numbers (C++)
- Binary Search on a text file (Java)
- read & write Binary data (C++)
- spaces in binary file not showing (C++)
- Another HotOffers Hijack (HJT log incl) (Viruses, Spyware and other Nasties)
- reading txt file into array (C++)
- How to load binary content of a .class file? (Java)
Other Threads in the C++ Forum
- Previous Thread: unknown error at run time urgent help needed
- Next Thread: Bowling game
| Thread Tools | Search this Thread |
6 api array based binary bitmap c++ c/c++ char class classes code coding compile console conversion count delete deploy desktop developer directshow dll download dwmapi dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph gui hangmangame happy homeworkhelp homeworkhelper iamthwee ifstream input int integer java job lib linkedlist linker loop looping loops map math matrix memory mobile modal multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference rpg snakes string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






