Thread: i want write contact records with linked list

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by phantomotap View Post
    And you think the only problem with a simply dump of the binary chunk the compiler may hand you in the face of the problems I referenced is supporting arbitrary lengths?

    Wow. I didn't mention arbitrary lengths anywhere. That is quite the jumped you've managed.
    Not really. Let's look at what you jumped to shall we?
    Quote Originally Posted by phantomotap View Post
    Sure... so long as he still manually corrects for the node pointers, doesn't expect that to work across platforms, doesn't add members, doesn't change the order of the members, doesn't add space to the character arrays, and doesn't change the character array to allocate for storage at runtime.

    Soma
    In little Billy's homework assignment, you have decided that:

    1. He needs platform independence.
    2. Has decided to reorder his structure after he's written his file.
    3. Decided to change the length of all his strings.
    4. Threw out all of his saved strings in lieu of making the user enter them every time the program loads instead.

    But it is "quite a jump" that I mention arbitrary lengths. Right?

    Yes, you do need to pay attention to what you have written, and if you want to change it, load it up and re-save it in the new format. Of course you do. The only time you would ever not need to do this immediately, would be if you instead went through the trouble of saving your data in keyword-data pairs, which is a huge amount of overhead compared to flat-file writing everything. (Which as this example will show, you can add provisions for missing pieces or pieces you no longer need.)

    And before you pretend it's not, let me just show you what you are going to involve:
    Code:
    while( (lineptr = freadline( fp )) != NULL )
    {
        word = getkeyword( lineptr );
        switch( tolower( word[ 0 ] ) )
        {
            case 'a':
                if( strcmp( "aardvark", word ) )
                {
                    myrecord->aardvark = getnumber( lineptr );
                    if( validrange( myrecord->aardvark, AARDVARKMIN, AARDVARKMAX )
                        foundaardvark = true;
                    else
                        log( "Read error. Got \'%s\': \'%s\'\n", word, lineptr );
                }
                if( strcmp( "apple", word )
                {
                    ...
                }
                ...
            case 'b':
                ...
        }
        if( !foundaardvark )
            myrecord->aardvark = aardvarkdefaultvalue;
        if( !foundapple )
            myrecord->apple = appledefaultvalue;
        ...
    }
    Oh yeah, that's much much better for cases where things will never change once they are finalized!

    You don't always need to care about your data being humanly readable, or that fault tolerant, or that flexible. Typically you never to care if "apple" comes in the file before "aardvark", because someone just decided they'd open your save file and swap two lines around, or add more text to a line, just because they felt like it. If someone goes in and mungs up your data, no you don't need to automatically assume it's valid, or test for validity. You don't need this kind of fault tolerance.

    Also, if you are in the process of making your application, then yes, you shouldn't be shocked when you decide to change your data around that your old flat file doesn't work pefectly. Why would you expect that? Even in the example I have provided here you still have to go in and manually add a case for "automobile" when you decide to add that field, or to make it ignore "aardvark" if you don't want it anymore. It doesn't magically just appear because I'm no longer using flat files. I don't really know what the point you were trying to make was.

    "Hey if you change your data, it will change!

    Of course it will! No one is saying that flat files magically fix this. Nothing does! No matter what possible way you can contrive your loading and saving of data, you will always have to load it up, fix it, and write it back out if you do any of the things you have said to look for. NONE of them let you skip this step. You ALWAYS have to fix your data up regardless of how you load or save it. Nothing you have mentioned has some data-safe-magic-bullet that lets you read and write it arbitrarily.


    Quzah.
    Last edited by quzah; 05-03-2011 at 04:02 PM.
    Hope is the first step on the road to disappointment.

  2. #17
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    you have decided that
    "I assumed that at some point he may want to do something more useful with the serialization of the data."

    There, I fixed that for you.

    Threw out all of his saved strings in lieu of making the user enter them every time the program loads instead.
    Got that one wrong. I didn't even come close to suggesting that. Care to try again?

    Yes, you do need to pay attention to what you have written, and if you want to change it, load it up and re-save it in the new format. Of course you do.
    I guess what I said about requiring code for both formats or a tool wasn't so silly then, huh?

    And before you pretend it's not, let me just show you what you are going to involve:
    That was a nice troll. I especially love how you continue to conflate my implication of a well specified binary file format (which, just so as you know since you clearly do not know, can be a flat binary file) with what is apparently a potentially unordered string based keyword-value pair.

    It isn't as good of a troll as what CommonTater though, so I'm going to have to give him the point.

    Hey if you change your data, it will change!
    But I will give you the point for "Statement of Obvious".

    I haven't mentioned any methods of serialization, nor did I imply that any given method should be used for any given case.

    I did two things.

    I finished the rest of the story, because without it, you and anyone else is flat out lying to the original poster in suggesting the code he posted.

    I said not to use a blind dump of whatever binary chunk the compiler gives you to attempt to serialize data because it is in no way reliably serializing the data. (You can even compile with a different set of compiler flags and the code may suddenly refuse to read the data.)

    You can continue to flail around with this if you want, but you'll still be wrong regardless of how you try and argue about the compilers binary chunk or conflate what I have stated with whatever strikes you as a useful argument.

    Soma

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by phantomotap View Post
    "I assumed that at some point he may want to do something more useful with the serialization of the data."

    There, I fixed that for you.
    You didn't fix anything. Your first post had one valid point:

    "You need to fix up your pointers if you are reading from a flat file into a node of a linked list."

    That's all. Nothing else you said had any relevance to anything anyone had said at that point. Nothing. Every point you think you made didn't matter to anything anyone else said.

    The rest of it was completely irrelevant. There was no dispute about "HUR HUR if you change the order of things your binary file is no longer useful!"

    No **** Sherlock! ANY FILE FORMAT is going to have to be converted if you change the structure around. It doesn't matter how you read and write it. You didn't disprove the usefulness of making flat files. You didn't somehow convince anyone that reading and writing to a linked list had some magical quality that you avoided when you didn't fwrite.

    You didn't have a point. Nothing you said made any difference to anything anyone had said at that time. There was no point. Your post was pointless, because no one said writing everything out as a flat file was some magic bullet that made you no longer have to care about the order things were read.

    As a matter of fact, no one even mentioned anything about changing the order of anything, or the size of any data. Your post was pointless, arguing a point that no one made, with no one, that no one cared about.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Posture, pose; posture, pose.

    You do more prancing than a sparkling vampire.

    First, I was silly because you conflated my statement with something no one said.
    Then, I was wrong because you didn't understand what I said.
    Then, I was wrong because you conflated my suggestion with a method of serialization no one referenced.
    And now, I'm wrong because I had no right to speak at all.

    Interesting... you remind me of Hunter.

    Well, as you've now given up to going "I'm right; you are wrong. neeneenerneer." I guess I'll drop this and leave the poor innocent newbie in your incapable hands.

    Soma

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Try actually making a point next time, and you'll save yourself the trouble of being wrong.

    OP: I am writing out like so...
    Tater: You can just write all of those at once.
    You: NUH UH! It will break when you completely change your data around and get rid of fields and add fields and completely change your structure!
    Me: No **** Sherlock.
    You: NUH UH! I made good points on teh internets!

    You were silly because you tried to make a point that was irrelevant to the statement being made.
    Then you were wrong, because you argued a point no one was disagreeing with.
    Then you were wrong, because again, you were arguing something no one cared about.

    And well, I'm right because I usually am. No one said anything about fwriteing a block of data at once being infallible. No one was arguing that if you used fwrite to write a block of data out that if you changed that data, that what you had written wouldn't have to be converted. Your entire point was "Hey, if you do what Tater said, when you change your structure all around it won't work to fread it back again!"

    Well guess what smart guy, if you did what the OP was doing you have to change that too. As a matter of fact, you have to change how you read and write every single time you change your structure around! Guess what, no one still cares, because no one was arguing that you didn't have to.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    May 2011
    Posts
    26
    i still could not do file operations. can you help more?

  7. #22
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Heh, I guess all you guys jumped off track a bit since the OP is still asking for help (despite the interesting debate).

    captain_turkiye I suggest you read this thread:

    Cprogramming.com FAQ > Why it's bad to use feof() to control a loop

    It should provide you with an example on how to do it the right way.
    1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
    2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
    3. Get rid of conio.h and other antiquated DOS crap headers.
    4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.

  8. #23
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I somewhat agree with phantomotap that simply dumping data is bad idea.
    Even for simple struct like
    Code:
      struct { int i; double f; };
    Different compiler may have different padding and such. There's no reliable way to read the file if the code is to be compiled with different compilers.

    Anyway I think OP needs text data processing. (probably this way he could learn, fscanf(), some parsing, etc).

  9. #24
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Bayint Naung View Post
    I somewhat agree with phantomotap that simply dumping data is bad idea.
    Even for simple struct like
    Code:
      struct { int i; double f; };
    Different compiler may have different padding and such. There's no reliable way to read the file if the code is to be compiled with different compilers.

    Anyway I think OP needs text data processing. (probably this way he could learn, fscanf(), some parsing, etc).
    That's a crap answer. There are countless thousands of video games out there that use binary files that flat dump their save files, and no, you aren't meant to be able to read them across all platforms. Games aren't the only thing, but it's something most people can relate to.

    Most people don't need platform independence for their homework problems. For that matter, most people don't need platform independence period. Wait, did I say games? No, I meant .bmp .png .doc .anyotherthingyoucanthinkofjustabout. It is preposterous to say that writing structures out is bad/never done.


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #25
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    Most people don't need platform independence for their homework problems
    Even for same platform with different compilers it may not work.

  11. #26
    Registered User
    Join Date
    May 2011
    Posts
    26
    i still don't solve my problem. can you help me. (i cut mail,address,city and zip)

    my text file like: name/surname/d.o.b
    name/surname/d.o.b

    Code:
    while(!feof(fp))
    	{
    
    		i=0;
    		while(c!='/')
    		{
    
    					c=fgetc(fp);
    					tmp->fname[i]=c;
    					i++;
    				}
    				tmp->fname[i]='\0';
    				printf("%s",tmp->fname);
    
    		fgetc(fp);
    
    		i=0;
    		while(c!='/')
    		{
    					c=fgetc(fp);
    					tmp->lname[i]=c;
    					i++;
    				}
    				tmp->lname[i]='\0';
    
    		printf("%s",tmp->lname);
    		fgetc(fp);
    		i=0;
    		while(c!='/')
    				{
    					tmp->born[i]=c;
    					c=fgetc(fp);
    					i++;
    				}
    		tmp->born[i]='\0';
    		printf("%s",tmp->born);
    		fgetc(fp);
    }

  12. #27
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You're doing this the hard way. You should look into fgets and sscanf or fscanf, or at the very least, use strtok to break up the lines you read from the file. Reading and copying character by character is a pain.

  13. #28
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Bayint Naung View Post
    Even for same platform with different compilers it may not work.
    Yet record (struct) based storage is in use world wide... Go Figure...

  14. #29
    Registered User
    Join Date
    May 2011
    Posts
    26
    Quote Originally Posted by anduril462 View Post
    You're doing this the hard way. You should look into fgets and sscanf or fscanf, or at the very least, use strtok to break up the lines you read from the file. Reading and copying character by character is a pain.
    how can i use this functions for my text file

  15. #30
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    By reading the documentation (try Googling them) and trying it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 03-02-2005, 02:45 AM
  2. read/write linked list
    By lambs4 in forum C Programming
    Replies: 4
    Last Post: 03-29-2003, 06:38 PM
  3. Replies: 4
    Last Post: 03-22-2002, 05:30 PM
  4. read/write random records from file
    By teja22 in forum C Programming
    Replies: 1
    Last Post: 09-27-2001, 05:19 AM
  5. read/write random records from file
    By Unregistered in forum C Programming
    Replies: 0
    Last Post: 09-27-2001, 03:01 AM