![]() |
| | #1 |
| Registered User Join Date: Sep 2008
Posts: 58
| How do I read in this as a string? 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 | |
| | #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 | |
| | #3 |
| and the Hat of Guessing 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 | |
| | #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 | |
| | #5 |
| CSharpener 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 | |
| | #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 | |
![]() |
| Tags |
| beginner, strings |
| Thread Tools | |
| Display Modes | |
|
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 |