Thread: Char Arrays

  1. #16
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Code:
    while(!fin.eof())
      {
       system("cls");
       for(i = 0; i < 500; i++)
       {
        szEdit[i] = new char[256];
       } 
       fin.getline(buffer, 256);
       strcpy(szEdit[i], buffer);
       fin.close();
      }
     }
    Lets follow the logic here a minute:
    Code:
    while not EOF
    do
      clear screen
      Loop i from 0 to 499
          szEdit[i] = new memory size(256)
      EndLoop
      get one line in buffer
      Copy buffer to szEdit[500]  /* Oops, out of bounds */
      close file
    end do
    - Whats the outer while loop for?
    - Note the out of bounds comment.

    Code:
    if(fout.fail())
      {
       printf("Error, Returning to main menu");
       menu();
      }
    It looks like you created a recursive problem here. Instead of calling menu(), did you mean to return instead?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  2. #17
    Registered User
    Join Date
    Aug 2002
    Posts
    10
    Ok, after spending most the night on this, I finally got it to work almost how it is supposed to, just one more problem.

    Code:
    void changename()
     {
      for(i = 0; i < 501; i++)
      {
       szEdit[i] = new char[256];
      } 
      i = 0;
      fin.open(profilename); 
      if(fout.fail())
      {
       printf("Error, Returning to main menu");
       menu();
      }
      while(!fin.eof())
      {
       fin.getline(buffer, 256);
       strcpy(szEdit[i], buffer);
       i++;
      }
      k = i;
      k++;
      i = 0;
      fin.close();
      while(i<k)
      {
       if (stricmp(fcontact, szEdit[i]) == 0)
       {
        printf("Enter the new name: ");
        
        cin.ignore(800, '\n');
        cin.getline(name, 256);
        strcat(econtact, name);
        strcpy(name, econtact);
        strcpy(szEdit[i], name);
        i = 0;
        fout.open(profilename);
        while(i<k)
        {
         fout << szEdit[i] << endl;
         i++;
        }
        fout.close();
       }
       else
       { 
        i++;
       } 
      } 
      fileoptions();
     }
    Now, when I run that, every once and awhile this will appear in the file that it saved too:

    TMP=c:\windows\TEMP

    I am unsure why this is happening.

    Also, how would I free up all the memory again?

  3. #18
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>Also, how would I free up all the memory again?
    You have to delete everything you've new'ed.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 08:21 AM
  4. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM