Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > C: String I/O Part 2

C: String I/O Part 2
Thread Tools
Syphor
Junior Member
Join Date: Jul 2002
Location: Australia
Status: Offline
Reply With Quote
Mar 5, 2004, 04:39 AM
 
Hey all thanks for the quick replies to part 1 of my post. Though it seems that I have stumbled onto another problem regarding strings

How can I insert this 1 character into another string. I thought this would work:
Code:
#include <stdio.h> int main() { FILE *fp; char fileOutput[255],test[255]; int i; fp = fopen( "data.dat", "rt" ); while( !feof(fp) ) { fgets( fileOutput, 255, fp ); sscanf( fileOutput, "%.1s", test ); printf("%s\n",test); } fclose(fp); return 0; }
Though the above code doesn't work
( Last edited by Syphor; Mar 5, 2004 at 05:12 AM. )
     
Chuckit
Clinically Insane
Join Date: Oct 2001
Location: San Diego, CA, USA
Status: Offline
Reply With Quote
Mar 5, 2004, 06:05 AM
 
I'm not sure I understand what you are trying to do. Are you trying to build up a string in test? Because if so, continually copying into it with sscanf will just keep overwriting what you did before. You'd need to increment a counter with the offset into the string (i.e. so that you're copying into test + counter).

At any rate, I don't think it's meaningful to scan a string with a length of 0.1.

And also, sscanf isn't really the best way to copy a string. You'd probably be better off looking at strncpy and strcat, though I'm still not entirely sure what you're trying to do.
Chuck
___
"Instead of either 'multi-talented' or 'multitalented' use 'bisexual'."
     
BLAZE_MkIV
Professional Poster
Join Date: Feb 2000
Location: Nashua NH, USA
Status: Offline
Reply With Quote
Mar 5, 2004, 12:20 PM
 
strstr or strchr to find the offset
then strncpy the front then your insert then strcat
     
   
 
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Top
Privacy Policy
All times are GMT -4. The time now is 08:51 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,