Thread: how can I load a file into a stuct

  1. #16
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    I do not quite get your point. you say I have a \n in the line. I think so too. It's at the end of the line. Am I supposed to have it in the line? I think I need \n for fgets to find out the line. And what do you mean by treat it as a part of the word? And the code is fine working with \n but I can not use it for " ".
    Last edited by timgao; 01-20-2007 at 12:45 PM.

  2. #17
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    I found out the problem above, I don't know why but the code works fine on another compiler.

    A new question how to insert string into a list.
    I tried numbers. It's fine. but not with string.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    struct node {
      char string;
      struct node *next;
    };
    
    int main()
    {   
        struct node *root;       
        struct node *conductor;  
    
        root = malloc( sizeof(struct node) );  
        root->next = 0;   
        root->string = "test string 1";
        conductor = root; 
        if ( conductor != 0 ) {
            while ( conductor->next != 0)
            {
                conductor = conductor->next;
            }
        }
    
        conductor->next = malloc( sizeof(struct node) );  
    
        conductor = conductor->next; 
    
        conductor->next = 0;         
        conductor->string = "test string 2";
        conductor = root;
        if ( conductor != 0 ) { 
            while ( conductor->next != 0 ) {
                printf( "%d\n", conductor->string );
                conductor = conductor->next;
            }
            printf( "%d\n", conductor->string );
        }
        return 0;
        
    }
    I try to do some test. but it prints out integers not string.

  3. #18
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    this code shouldn't work
    string is char - it cannot store the string

    it should be char* and dinamically allocated or char string[BUF_SIZE]

    and you should use string manipulation functions like strcpy
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #19
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    inside struct I need to have :

    Code:
    struct node {
      char *string;
      struct node *next;
    };
    can you give me some more details about strcpy(structure I can find out)?
    I mean where shall I use it?

    here?
    Code:
    conductor->string = "test string 2";
    How do I copy string to the node?

  5. #20
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    http://msdn2.microsoft.com/en-us/lib...63(VS.80).aspx

    make sure that you have enough room for the string to copy to.
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #21
    Registered User
    Join Date
    Jan 2007
    Posts
    11
    That is just about strcpy itself. How can I use this to insert string to the struct?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM