How do I read in this as a string?

This is a discussion on How do I read in this as a string? within the C Programming forums, part of the General Programming Boards category; I have a file that I can already read in If the file says: Jamie 20 I use this code: ...

  1. #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.

  2. #2
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262
    Two solutions (I recommend the one at the bottom) http://faq.cprogramming.com/cgi-bin/...&id=1043284351 gets() and fgets()

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    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
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #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

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    You can use isdigit() from <ctype.h>
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 1
    Last Post: 05-30-2003, 03:31 AM
  4. string handling
    By lessrain in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 08:36 PM
  5. read records fron file into a binary tree
    By Kirsten in forum C Programming
    Replies: 1
    Last Post: 04-23-2002, 03:48 PM

Tags for this Thread


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