Thread: Whats wrong with this code?

  1. #16
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    Originally posted by skipper
    >I gotta get this dam project done by tommorrow cuz its due!


    >Mabe some of you guys could fix some of my code so it's proper and may even work.
    C'mon.

    -Skipper
    cmon what? This forum is used to help and others helping in eachothers code and problems. No harm in asking to help fix my code

  2. #17
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    i would help, but i cant thats the thing. i know nothinbg right now of classes and strcutures and unions and stuff like that.

    if i could i would but i cant.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

  3. #18
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Attach the file you need to use with this code. "assign6", I'll take a look.

  4. #19
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    OK here

  5. #20
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    did ya get it?

    anyone else on .02?

  6. #21
    TransparentMember correlcj's Avatar
    Join Date
    Jun 2002
    Posts
    378

    dyou opened the stream...

    I see you opened the strean but did not read ->ifstream , write->ofstream or close() the outsteam in your code. I think the problem lies after here

    fp = fopen("assign6", "wb");
    Also, I think you have to have the filename extesions added to assign6.txt like so...

    Thats all I can really help since i am learning about <fsteam> class myself but I believe i am correct.
    I hope this helps.
    cj
    "Be formless, shapeless, like water... You put water into a cup, it becomes the cup, you put water into a bottle, it becomes the bottle, you put it in a teapot, it becomes the teapot... Now water can flow, or it can crash, be water my friend."
    -Bruce Lee

  7. #22
    Registered User
    Join Date
    Oct 2002
    Posts
    19
    well now im getting this

    C:\Program Files\Microsoft Visual Studio\assign6\assign6.cpp(161) : error C2665: 'basic_ifstream<char,struct std::char_traits<char> >::basic_ifstream<char,struct std::char_traits<char> >' : none of the 3 overloads can convert parameter 2 from type '
    char [3]'
    C:\Program Files\Microsoft Visual Studio\assign6\assign6.cpp(186) : error C2065: 'dls_store' : undeclared identifier

    from this code:

    Code:
    /* Load the address file. */
    void load()
    {
      struct record *info;
      FILE *fp;
    
      fp = ifstream("assign6", "rb");
      if(!fp) {
        cout << ("Cannot open file.\n");
        exit(1);
      }
    
    
      /* free any previously allocated memory */
      while(start) {
        info = start->next;
        free(info);
        start = info;
      }
    
      /* reset top and bottom pointers */
      start = last = NULL;
    
      printf("\nLoading File\n");
      while(!feof(fp)) {
        info = (struct record *) malloc(sizeof(struct record));
        if(!info) {
          printf("Out of Memory");
          return;
        }
        if(1 != fread(info, sizeof(struct record), 1, fp)) break;
        dls_store(info, &start, &last);
      }
      fclose(fp);
    }

  8. #23
    Registered User The Dog's Avatar
    Join Date
    May 2002
    Location
    Cape Town
    Posts
    788
    >> FILE *fp;
    >> fp = ifstream("assign6", "rb"); //Dragons be here

    Leave the code as it was :
    Code:
    fp = fopen("assign6.txt", "rb");
    if(!fp) {
        cout << ("Cannot open file.\n");
        exit(1);
    }
    Also, in your while loop, try not to use feof() as the loop condition, instead use fread(), like so :
    Code:
    info = (struct record *)malloc( sizeof(struct record) );
    if ( !info ) return;
    while( 1 == fread(info, sizeof( struct record ), 1, fp ) ) ) {
          dls_store(info, &start, &last);
    }
    And about this :
    Code:
    /* free any previously allocated memory */
    while(start) {
        info = start->next;
        free(info);
        start = info;       //why ???
    }
    
    //Did you mean to do this?
    info = start;
    struct record *temp;
    while( info ) {
        temp = info;
        info = info->next;
        free(temp);
    }
    If you still have problems, then it's probably in your dls_store() function.

  9. #24
    Emotionally Unstable DarkViper's Avatar
    Join Date
    Oct 2002
    Posts
    343
    LOL! decided which language you want to program nice one.
    ~DJ DarkViper signing out
    ----------------------------------------
    My Site:
    Black Jaguar Studios

    Languages:
    Fluent English, Starter German, HTML, Javascript, Actionscript, Intermediate PHP

    Verteran Despiser of: ASP, Java, BASIC, Pascal, Cobalt

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM