function that returns a string...

i would like to have a function (actually my first one) that returns me a string.

i have some really basic questions:

1.) i know my resulting string will be 80 letters long. so the box i put it in must be 81? for the end of string thing?
2.) would this be the apropriate way to declare such a function: or is there something better than char*
char* extractheaderstring(); ?
3.) lets say my string contains backslashes since its from a text file or whatever and i have no control what it contains, how do i deal with this, so i can cout the string without triggering magic backslash commands automatically?


Last edited on
Use C++ strings instead:
http://www.cplusplus.com/reference/string/string/

Much easier to use and less chance on errors!

ok i created something with string now, but im not happy yet... this surely goes easier?

1
2
3
4
5
6
7
8
9
10
{
    string buffer;
    char* charbuff;
    for (int i = 0; i<80; i++) { // since i want 80 letters in my string
    FileBin.seekp(i);
    FileBin.read(charbuff,1);
    buffer.append(charbuff);
    }
    cout <<"buffer is: "<< buffer; //checking content of buffer now...
}


see screenshot for result:

http://screencast.com/t/llwnjirb9m80
Last edited on
1
2
3
4
5
6
7
fstream FileBin("S:\\Coding\\codeblocks\\spielereien\\20110927_001\\settingup\\datei2.dat", ios::in|ios::out|ios::binary); //?
if (FileBin.is_open())
{
    char* buffer;
    FileBin.getline(buffer,80);
    cout <<"buffer is: "<< buffer;
}


trying to make the code better with getline but it causes an error that i dont understand:

see screenshot:

http://screencast.com/t/LAKSQMAsPY
char*s are not strings! They're pointers, and they don't mean anything unless you actually have them point to something.

Save yourself a ton of headaches and forget about char*. Just use strings.

1
2
3
4
5
6
7
// get rid of this crap
//    char* buffer;
//    FileBin.getline(buffer,80);

// replace it with this:
    string str;
    getline(FileBin,str);
thanks for trying to help :)

remember im a total newbie and yes i would love to see a "good" way to do what i want to have.

but unfortunatly your code does not work at all,

it tells me getline is unknown name

here is the code i tried out after your posting:
1
2
3
4
5
6
7
8
fstream FileBin("S:\\Coding\\codeblocks\\spielereien\\20110927_001\\settingup\\datei2.dat", ios::in|ios::out|ios::binary); //?
if (FileBin.is_open())
{
	string str;
    getline(FileBin,str);
    cout <<"buffer is: "<< str;

}


as you can see i replaced my crap with your two lines
Last edited on
it tells me getline is unknown name


Try adding <iostream> to your #includes.


EDIT: but you must have iostream included if you're using cout....

that error makes no sense. Can you post the actual error you're getting? (the whole thing -- don't paraphrase/summarize)
Last edited on
http://screencast.com/t/9cWwEjEWs1R

hope it helps - its german :(

closed account (zb0S216C)
You need to include <string> in-order for std::cout to understand how to handle a std::string object.

Just to clarify:

1) "String" isn't a string.
2) "String\0" is a string.
3) char* is a pointer to a character. It's not a string.
4) char *String = "String\0" is a pointer to a string literal.

Wazzak
Last edited on
ok the string include worked wonders... i didnt have it included because microsoft visual c++ 2010 "auto corrected" #include <string> to #include <specstrings.h>

now the code "magically" works but i have not really understood why and how.

i am reading a file bytewise, because it is not a .txt file. i know from the file only that the first 80 byte are unsinged chars.

i want them in a string.

getline "seems" to do the magic.
but what tells getline that it has to read 80 chars? and what must i do if i only want 40? or 10? or 200?

thanks!
im really desperate to understand that, can someone help?

how or what tells getline to get 80 unsigned chars from the unknown file and put them in a string?

Topic archived. No new replies allowed.