how can I load a file into a stuct

This is a discussion on how can I load a file into a stuct within the C Programming forums, part of the General Programming Boards category; I do not quite get your point. you say I have a \n in the line. I think so too. ...

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    http://msdn2.microsoft.com/en-us/lib...63(VS.80).aspx

    make sure that you have enough room for the string to copy to.
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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?

Page 2 of 2 FirstFirst 12
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, 11: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, 08:51 PM
  3. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 11:51 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11: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

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