Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Using a String as a Stream instead of a File ?

Status
Not open for further replies.
May 13, 2002
75
GB
Hi,

again, i'm going to have to excuse myself for not being a C programmer, but i'm putting together something i need using someone else's code (always fun).

I have some code that takes file and runs though it doing some conversions etc. I need to use these conversions but instead of having a file, i have the text i need converting stored in a string.

What i don't really want to do (but it should work) is write my string out to a file, get a pointer to the file and use that. Is there a better way ?

So...

Code:
char *str;
FILE *Fp;
Fp = fopen("myfile.rdf", "r");
Then functions are called that use Fp for instance
Code:
while(NULL!=fgets(line,MX_LINE,Fp)) 
  ...
and some other functions that need a file pointer.

I want to use a string str instead of Fp. Is this possible, or should i write my string out to a file ?

Thanks for any advice


Alistair [monkey]
 
1) you need to allocate some space for the string

str = malloc (strlen (line) + 1);

2) Copy the string

strcpy (str, line);

Is that what you're looking for? Alternatively, write out how you would do it in C++ with strings, iostreams, std algorithms etc and I'll translate it to C for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top