Thread: load file to structure

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    92

    Question load file to structure

    Hello, I am working on function that builds the linked list of nodes, program exits when it comes to the line where file needs to be opened. don't understand why? To the function I am passing pointer to the new list and a file name. I was trying to find the answere myself.....with no success

    I also have another question, when I am passing linked list to the function and when I need to loop through it or somthing else.....does it always start from a first node ...head or I need to tell the function from where in a linked list to sart to do whatever .....if yes, can I do it like k=NULL, and then k=k->next?


    thank you in advance

    Code:
    #include<stdio.h>
    #include"keytype.h"
    
    void load(KEYS k,char *fn)
    {
    	FILE* fp;
    	k=(KEYS*)malloc(sizeof(KEYS));
    	
    	if((fp=fopen(fn, "r"))== NULL)
    	{
    		printf("Error, the file does not exist");
    		exit(1);
    	}
    
    	if(ftell(fp)==NULL)
    		exit(2);
    	
    	fscanf(fp, "%s", k->addressee);
    	fscanf(fp, "%s", k->sender);
    	fscanf(fp, "%s", k->regarding);
    	fscanf(fp, "%d%*c%d%*c%d", &k->date.month, &k->date.day, &k->date.year);
    	fscanf(fp, "%d", &k->id);
    	fscanf(fp, "%s", k->fname);
    
    	while(!feof(fp))
    	{	
    		k->next=(KEYS*)malloc(sizeof(KEYS));
    		k=k->next;
    		
    		fscanf(fp, "%s", k->next->addressee);
    		fscanf(fp, "%s", k->next->sender);
    		fscanf(fp, "%s", k->next->regarding);
    		fscanf(fp, "%d%*c%d%*c%d", &k->next->date.month, &k->next->date.day, &k->next->date.year);
    		fscanf(fp, "%d", &k->next->id);
    		fscanf(fp, "%s", k->next->fname);
    	}
    	fclose(fp);
    
    
    	
    	while(k->next!=NULL)
    	{
    		k=k->next;
    		printf("%s %s %d\n", k->next->addressee, k->next->sender, k->next->id);
    
    	}
    }

    Thank you.
    Last edited by nynicue; 08-05-2009 at 07:05 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You have not included the include file for malloc(), stdlib.h. The error you would normally receive is masked by your cast in that line of code. You don't need to cast the results of malloc(), in C.

    Is fn a constant char * to a valid file?

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you for the reply Adak , stdlib.h doesn't change anything......in the main fn delcared as char fn[CHARS];......I am sorry but can you explain what cast the resolts of malloc() means?

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is the cast (in red):
    Code:
    k=(KEYS*)malloc(sizeof(KEYS));
    Casts are sometimes needed in C, but never for the return value of malloc(). <<== Eh, maybe not, see note in blue

    With the cast, most compilers will not give you the error that you do not have the right include file (stdlib.h), in place.

    I'm going to retract what I said about casting, in this case. You don't need to do any casting of malloc()'s result for a *standard* data type, but for a struct, you may need to cast. -- Sorry for the confusion, but I'm not familiar with casts made from malloc's pointer, to struct's.


    It would be a good idea to test the return value of malloc(), and ensure that it has done it's job, correctly.

    When you step through the program, you should see exactly what line of code it's failing on. The question is not if fn is a constant char *, but is fn a constant char * to a *file*.

    Have you checked that file with an editor to see if it can be read?
    Last edited by Adak; 08-05-2009 at 08:54 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Adak
    I'm going to retract what I said about casting, in this case. You don't need to do any casting of malloc()'s result for a *standard* data type, but for a struct, you may need to cast. -- Sorry for the confusion, but I'm not familiar with casts made from malloc's pointer, to struct's.
    Actually, your original assertion was correct: in C, a pointer to void is implicitly convertible to a pointer to any other pointer to an object type. As such, the return value of malloc need not be cast to be converted to a pointer to a struct type.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    "I also have another question, when I am passing linked list to the function and when I need to loop through it or somthing else.....does it always start from a first node ...head or I need to tell the function from where in a linked list to sart to do whatever .....if yes, can I do it like k=NULL, and then k=k->next?
    "
    you are pointing to th head and iterate it till head reaches null

    Code:
    for(temp=head;temp;temp=temp->next)
    {
    
    }

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    60
    while not using fread() to read the whole struct instead of reading each elements?

  8. #8
    Banned
    Join Date
    Oct 2008
    Posts
    1,535
    if you are reading from a file
    then just use fscanf and then but to linked list like i told you

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Quote Originally Posted by laserlight View Post
    Actually, your original assertion was correct: in C, a pointer to void is implicitly convertible to a pointer to any other pointer to an object type. As such, the return value of malloc need not be cast to be converted to a pointer to a struct type.
    Once and for all, malarkey!
    For code such as:
    Code:
    ADDRESS_TABLE_DEF *at;
    ...
    at = malloc(sizeof(ADDRESS_TABLE_DEF));
    I get:

    ...\visual studio 2005\projects\test\test\test.cpp(1548) : error C2440: '=' : cannot convert from 'void *' to 'ADDRESS_TABLE_DEF *'

    Error goes away when I do:
    Code:
    	at = (ADDRESS_TABLE_DEF *)malloc(sizeof(ADDRESS_TABLE_DEF));

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    That's because you are using a C++ compiler, not a C compiler. Rename that file from test.cpp to test.c and see what happens...
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonoob
    Once and for all, malarkey!
    That implies that you are compiling as C++, not as C. If you need to be compatible with C++, then go ahead and use that cast.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Thanks! I'm sure you are right, bithub and laserlight.

    I can't test it out because renaming test.cpp to test.c means I break everything. If I double click 'test.c', the Visual Studio opens to show me the file, but there is no green arrow (it's grey) to compile/build. I hate fancy programming font ends.

    Anyway. I believe you. Microsoft (and every other fancy programming tool is evil).

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by nonoob
    I can't test it out because renaming test.cpp to test.c means I break everything. If I double click 'test.c', the Visual Studio opens to show me the file, but there is no green arrow (it's grey) to compile/build.
    It might be because test.c is not included in the project, though that should not be the case if you rename using the IDE. Anyway, you can force compilation as C even with a .cpp file extension. Go to Project -> Properties -> Configuration Properties -> C/C++ -> Advanced. In the "Compile As" field, select "Compile as C Code (/TC)".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nonoob View Post
    Thanks! I'm sure you are right, bithub and laserlight.

    I can't test it out because renaming test.cpp to test.c means I break everything. If I double click 'test.c', the Visual Studio opens to show me the file, but there is no green arrow (it's grey) to compile/build. I hate fancy programming font ends.

    Anyway. I believe you. Microsoft (and every other fancy programming tool is evil).
    If you care: VS will only compile things that are in projects. Put your test.c file in a project, you'll be able to build it, and away you go.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    92
    Thank you , I just checked.....I putted printf's after each line to see where program crashes , and it do that on if(ftell(fp)==NULL) line, on this line I was checking to see if the file is emty.

    I know that file is not empty, then why it is exits there?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File send app - md5 checksum different when load is put on server.
    By (Slith++) in forum Networking/Device Communication
    Replies: 5
    Last Post: 12-31-2007, 01:23 PM
  3. multiple file loading. so fruturated! help!
    By psychopath in forum Game Programming
    Replies: 5
    Last Post: 05-09-2005, 05:13 PM
  4. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM