FSCANF quick question :)

Hi guys.
Writing to a file I found this issue.
I want to use fscanf
and i go:

1
2
string name = "Luca";
fscanf( pFile, "%s", name );


This does NOT work because 'name' should be an array of characters and NOT a std::string in order to be accepted as argument.

How can I do?
Can anybody point me in the right direction?
I really need to pass a string to a file. Do you guys know of another function similar to fprintf that accepts std::string ? thanks

Clodi
I don't understand why you would use fscanf with a std::string. Why not just use an ifstream and the usual C++ formatted input.

However, to answer your question, you could use a character array instead of a string with fscanf. Make sure the array is big enough to hold the string being read in.
 
char name[100] = "Luca";

Why not just use an ifstream and the usual C++ formatted input.


Thank you so much!!
What is that "ifstream" and "usual C++ formatted input" ?
Could you please give me a three-line example of how to use it?


And, no. I really need to use std::string and no raw arrays

Thanks
Clodi
Well, are you familiar with using cin to get input? That is an example of an input stream, which gets its data from the keyboard. An ifstream can do more or less the same thing, but takes its input from a file instead.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>

    using namespace std;

int main()
{
    ifstream fin("input.txt");
    string word, line;
    fin >> word;
    getline(fin, line);
    
    cout << "word = " << word << endl;
    cout << "line = " << line << endl;
    
    return 0;
} 
Thanks a lot!!
I will try to implement that tomorrow!

Clodi
closed account (Dy7SLyTq)
while i agree with above, to answer your first question: the string class has a method called c_str() which returns a char* of the string it currently holds
@ DTSCode

the string class has a method called c_str() which returns a char* of the string it currently holds


Thank you so much!!
How can I use it? do I simply go something like:

1
2
3
4
5
6
string name = "Mario";
int array[ 5 ] = name.c_str();

..or..

int array[ 5 ] = c_str( name );

?

@Chervil

Thank you for the precious help.
I've tried to implement your code to my app but I can't figure out how to scan and skip characters..

Could you please show me how to do what I have been doing with "fscanf" and "fseek" ?

Thanks a lot,
Clodi
Last edited on
closed account (Dy7SLyTq)
kind of. a) i wouldnt make it an int array, because it returns a char*. if you need to convert to int i would use stringstream b)and this is just preference i dont know if there is a real danger to doing it char array[] = name.c_str(), but i would do char *array = name.c_str()
I tried but if I go:

1
2
3
string hello;
char* Array[ 10 ];
Array = hello.c_str();


it gives me this error:

cannot convert from 'const char *' to 'char *[10]'



Am I doing anything wrong here?
Last edited on
closed account (Dy7SLyTq)
char *array not char *array [10]
Thank you!

Sorry, didn't notice it's a pointer XD

It must be:
const char* pArray

otherwise it complains,

now it works like a charm!

thank you,
Clodi
we have transformed a std::string into an array of characters..



Now what if I wanted to transform an array of characters into a std::string? :)
Is it possible?

Thanks,
Clodi
Now what if I wanted to transform an array of characters into a std::string? :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <string>
#include <cstring> // for strcpy etc.

using namespace std;

int main()
{
    char array[20] = "Fred";
    string str;
    
    str = array;
    cout << "1 array : " << array << endl;
    cout << "1 str :   " << str   << endl;
    
    str = "Bloggs";
    strcpy(array, str.c_str());
    cout << "2 array : " << array << endl;
    cout << "2 str :   " << str   << endl;
       
    return 0;
}

Could you please show me how to do what I have been doing with "fscanf" and "fseek" ?

I don't know what you were doing, I don't think you made that clear.
But rather than try to convert your old code into new code, it might be better if you just describe what it is you want the program to do.
Thanks a lot!!

Don't worry!! I am doing great ( lol )
I'll send you my code once it's done as I am sure you'd have many things to say about the way I code and I can't wait to hear them as I don't want to pick up bad coding habits from the start

Thanks again,
I ll let you know soon how it goes

CLodi
Topic archived. No new replies allowed.