C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 09-14-2008, 09:39 AM   #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"

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-14-2008, 10:03 AM   #2
The superheterodyne.
 
twomers's Avatar
 
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   Reply With Quote
Old 09-14-2008, 10:27 AM   #3
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,353
Quote:
Two solutions (I recommend the one at the bottom) http://faq.cprogramming.com/cgi-bin/...&id=1043284351 gets() and fgets()
Not gets(), of course, but fgets(). However, that assumes that you know the length of the name, but here it is unknown.

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   Reply With Quote
Old 09-14-2008, 11:24 AM   #4
Registered User
 
Join Date: Sep 2008
Posts: 58
Quote:
Originally Posted by laserlight View Post
Not gets(), of course, but fgets(). However, that assumes that you know the length of the name, but here it is unknown.

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.

How do i code "if next character is an interger"?
like if (x == int)

i know thats not right.. haha
scarlet00014 is offline   Reply With Quote
Old 09-14-2008, 11:28 AM   #5
C++ Witch
 
laserlight's Avatar
 
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   Reply With Quote
Old 09-14-2008, 11:39 AM   #6
subminimalist
 
MK27's Avatar
 
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);
}
If you can't change anything about the input file, you'll have to do the character by charater method (and hope that no one has a wierd name like "Bob 35"). Beware: I don't know what the non-GNU equivalent of getline is.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Reply

Tags
beginner, string, strings, struct, structs

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 11:35 PM.


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