C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old August 6th, 2006, 04:05 PM
Rossss Rossss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: UK
Posts: 3 Rossss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 44 sec
Reputation Power: 0
A "." in a char* crashes it

Hi, i have a very simple problem that im pulling my hair out over!
  1. The user enters a filepath to a char*
  2. It reads data in that file to a structure with ifstream
  3. It then outputs data from the structure to several files with ofstream
  4. When it exits the ofstream loop the program is finished, or it should be
  5. If the file being read is "c:\file" it ends the program no problem
  6. But if the file being read is "c:\file.txt" it crashes between the last loop and the final } used to end the program

In the CPU debug window it stops at "mov ebx,[ebx+0x04]"
when it stops, the ebx register is empty, just shows 00000000

It works fine in the built .exe just not when i run it from borland, but i dont want memory leaks, etc in the final version

I've never had this problem before, it doesnt even seem to make sense

Last edited by Rossss : August 6th, 2006 at 04:25 PM. Reason: extra info

Reply With Quote
  #2  
Old August 6th, 2006, 05:24 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Could you post the source code?

Preferably between [code] or [highlight=C++] tags :-)
__________________
This is my code. Is it not nifty?

"The biggest problem encountered while trying to design a system that was completely foolproof, was, that people tended to underestimate the ingenuity of complete fools."
---Douglas Adams


Join the Itsacon fanclub!    
Zero Tolerance: Spammers banned so far: 280

Reply With Quote
  #3  
Old August 6th, 2006, 06:39 PM
Rossss Rossss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: UK
Posts: 3 Rossss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 44 sec
Reputation Power: 0
Sure, here it is;
C++ Code:
Original - C++ Code
  1. /*
  2. Title:          PageMaker
  3. Description:    Takes a wordlist (path given by user) & creates an
  4.                 empty web page for every line in the wordlist.
  5.                 Every web page is named from the word read out in the wordlist.
  6.                 All entrys in wordlist must be on a seperate line
  7. */
  8.  
  9. #include <stdio.h>
  10. #include <fstream.h>
  11.  
  12. struct word
  13. {
  14.         char newFile[100];
  15. };
  16.  
  17. void main()
  18. {
  19.         struct word word1[1000];
  20.         char* path;
  21.  
  22. //----------------------------------------------------------------------
  23. //Get File Path
  24.  
  25.         printf("\nPlease enter the location of your wordlist:  ");
  26.         scanf("%s", path);
  27.  
  28.         char * dirname  = path;
  29.         //char * filename = "wordlist.txt";
  30.         char * tmpdirname;
  31.  
  32.  
  33.         // Length of new string includes directory
  34.         // separator and null character
  35.         int newlen = strlen(dirname) + 2;
  36.         tmpdirname = (char *) malloc(newlen);
  37.         if (tmpdirname == NULL)
  38.         {
  39.                 // Handle memory error.
  40.         }
  41.  
  42.         strcpy(tmpdirname, dirname);
  43.  
  44.         dirname = tmpdirname;
  45.  
  46. //----------------------------------------------------------------------
  47. // Read File In
  48.  
  49.         ifstream infile(dirname);
  50.  
  51.         if (infile.fail())
  52.         {
  53.                 printf("Sorry, input file not available\nEnsure there is a file called %s in the same directory as this program & try again", dirname);
  54.                 getchar();
  55.                 getchar();
  56.                 exit(1);
  57.         }
  58.  
  59.         int i=1;
  60.         while (!infile.eof())
  61.         {
  62.  
  63.                 infile.getline(word1[i].newFile, 100);
  64.                 printf("%s\n", word1[i].newFile);
  65.                 i++;
  66.         }
  67.         infile.close();
  68.  
  69. //--------------------------------------------------------------------
  70. // Create Files From 'word' Struct
  71.  
  72.         //Every loop cycle creates a new file from that particular
  73.         //structure entry, and appends '.html' to the filename
  74.         for (int j=1; j != i; j++)
  75.         {
  76.  
  77.                 string tempword2 = word1[j].newFile;
  78.  
  79.  
  80.                 char* outfilename = word1[j].newFile;
  81.                 outfilename = strcat(outfilename, ".html");
  82.  
  83.                 ofstream outfile(outfilename);
  84.                 if (outfile.fail())
  85.                 {
  86.                         printf("Unknown Output Error\nProgram will now end\n");
  87.                         getchar();
  88.                         getchar();
  89.                         exit(1);
  90.                 }
  91.  
  92.         //HTML Head
  93.         outfile << "<html><head>";
  94.         outfile << "<title>" << outfile << "</title>";
  95.         outfile << "<meta name=\"keywords\" content=\"\" >";
  96.         outfile << "<meta name=\"description\" content=\"\" >";
  97.         outfile << "</head>";
  98.  
  99.         //HTML Body
  100.         outfile << "<body>test line - working?";
  101.         outfile << "</body></html>";
  102.  
  103.         //Close outfile
  104.         outfile.close();
  105.  
  106.         }
  107.  
  108.         printf("\n\n\nTask Completed!");
  109.         getchar();
  110.         getchar();
  111. }
  112.  
  113.  
  114.  

Reply With Quote
  #4  
Old August 7th, 2006, 01:45 AM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Ah, problem found.

You can't just write a string to an uninitialized pointer!.

Either declare it as
Code:
char[256] path;


or put a
Code:
char = malloc(256 * sizeof(char));
before the scanf.

It's a wonder your system hasn't BSOD'ed, you're writing the string to a completely random place in memory!
Comments on this post
ubergeek agrees: as long as you meant char path[256];

Reply With Quote
  #5  
Old August 7th, 2006, 12:36 PM
ubergeek ubergeek is offline
Contributing User
Dev Articles Novice (500 - 999 posts)
 
Join Date: Jan 2005
Posts: 600 ubergeek User rank is Private First Class (20 - 50 Reputation Level)ubergeek User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 2 Days 22 h 40 m 27 sec
Reputation Power: 4
Send a message via AIM to ubergeek
correction:

char path[256];
path = malloc(256 * sizeof(char));

Reply With Quote
  #6  
Old August 7th, 2006, 01:37 PM
Itsacon's Avatar
Itsacon Itsacon is offline
Command Line Warrior
Click here for more information
 
Join Date: Aug 2004
Location: Sector ZZ9 Plural Z Alpha
Posts: 1,001 Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)Itsacon User rank is Lance Corporal (50 - 100 Reputation Level)  Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3Folding Points: 1000232 Folding Title: Super Ultimate Folder - Level 3
Time spent in forums: 6 Days 15 h 6 m 59 sec
Reputation Power: 6
Send a message via ICQ to Itsacon
Quote:
Originally Posted by ubergeek
correction:

char path[256];
path = malloc(256 * sizeof(char));



Oops, you're right. Turned those around...

That's what you get for posting in a rush, thanks for correcting me.

Reply With Quote
  #7  
Old August 7th, 2006, 03:00 PM
Rossss Rossss is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Aug 2006
Location: UK
Posts: 3 Rossss User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 59 m 44 sec
Reputation Power: 0
Excellent!
Thanks guys it works a treat, i thought it had to be some small silly thing i'd done, duh!

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > A . in a user entered char* crashes it


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump