![]() |
| | #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" 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 | |
| | #2 |
| The superheterodyne. Join Date: Dec 2005 Location: Ireland
Posts: 2,205
| Two solutions (I recommend the one at the bottom) http://faq.cprogramming.com/cgi-bin/...&id=1043284351 gets() and fgets()
__________________ I blag! |
| twomers is offline | |
| | #3 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,353
| Quote:
One way would be to scan character by character. If you encounter a character that is valid for a name, you copy it to the name string. If you encounter whitespace, you do not copy it over until you encounter a non-whitespace character, in which case you either decide to add the space and then the non-whitespace character, or if the non-whitespace character is a digit, you begin reading in the age (?). This is painfully manual though, so perhaps someone would be able to suggest a better method.
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #4 | |
| Registered User Join Date: Sep 2008
Posts: 58
| Quote:
How do i code "if next character is an interger"? like if (x == int) i know thats not right.. haha | |
| scarlet00014 is offline | |
| | #5 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,353
| You can use isdigit() from <ctype.h>
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #6 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| If you can modify the input file, use a delimiter other than a space and break each line in half with strtok: Jamie lynn:20 Eric D. Mcdaniel:40 Joe:23 Code: #include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
short int i=0;
size_t len;
char *line=NULL, *tok, DL[]=":\n";
FILE *fstRO=fopen("/root/test/test.txt", "r");
struct {char *name;
int age;
} *examp;
examp = malloc(sizeof(*examp));
while((getline(&line,&len,fstRO)) != -1) {
tok=strtok(line,DL);
if (i>0) examp = realloc(examp, (i+1) * sizeof(*examp));
examp[i].name=malloc(strlen(tok)+1);
strcpy(examp[i].name,tok);
tok=strtok(NULL,DL);
examp[i].age=atoi(tok);
i++;
}
free(line);
fclose(fstRO);
for (i=0;i<3;i++) printf("%s is %d\n", examp[i].name,examp[i].age);
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
![]() |
| Tags |
| beginner, string, strings, struct, structs |
| 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 |