C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-13-2008, 09:39 PM   #1
Registered User
 
Join Date: Sep 2008
Posts: 58
How do I read in this as a string?

I have a file that I can already read in

If the file says:

Jamie 20

I use this code:
fscanf(file,"%s %lf", struct_ptr[1].name, &struct_ptr[1].number);

struct_ptr[1].name = "Jamie" and then struct_ptr[1].number = 20

so... what if I had:

Jamie Lynn 20

I dont know how to make struct_ptr[1].name equal the whole string "Jamie Lynn"
scarlet00014 is offline   Reply With Quote
Old 09-13-2008, 09:51 PM   #2
Registered User
 
Join Date: Jan 2008
Posts: 66
You can try this where 20 in %20 is the length of string you need to read.
But this isn't safe so you should probably try an another method.

fscanf(file,"%20s %lf", struct_ptr[1].name, &struct_ptr[1].number);
ThLstN is offline   Reply With Quote
Old 09-13-2008, 09:57 PM   #3
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,740
That's still going to stop at spaces. There are scansets, so you could do something like %<some number>[^0123456789] to read in up to a digit. (The <some number> is the size of the buffer .name.) Granted, you'll get the space at the end, but that's easily fixable.
tabstop is offline   Reply With Quote
Old 09-13-2008, 10:01 PM   #4
Registered User
 
Join Date: Sep 2008
Posts: 58
no thats not the only name in the file

I have like:
Jamie lynn 20
Eric D. Mcdaniel 40
Joe 23

so I need to read in the name for the name part of the struct, then the number for the number part of the struct.
scarlet00014 is offline   Reply With Quote
Old 09-13-2008, 10:56 PM   #5
CSharpener
 
vart's Avatar
 
Join Date: Oct 2006
Posts: 5,242
read whole line into temp buffer using fgets
use strrchr to find last space
copy the part before space into name
convert part after space using strtol into number
vart is offline   Reply With Quote
Old 09-14-2008, 03:32 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
What vart suggests will certainly work. Another method would be to change the input file format so that you have:
Code:
"Joe", 20
"Jimmy Hoffa", 21
"E.W. Bloggs", 23
"Eric John Jones-Smithe", 46
--
Mats
__________________
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
matsp is offline   Reply With Quote
Reply

Tags
beginner, strings

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
We Got _DEBUG Errors Tonto Windows Programming 5 12-22-2006 05:45 PM
Message class ** Need help befor 12am tonight** TransformedBG C++ Programming 1 11-29-2006 11:03 PM
read string with fgets followed by another string needed to read Yourhighness C Programming 1 05-30-2003 02:31 AM
string handling lessrain C Programming 3 04-24-2002 07:36 PM
read records fron file into a binary tree Kirsten C Programming 1 04-23-2002 02:48 PM


All times are GMT -6. The time now is 04:25 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22