Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 21st, 2008, 9:30 PM   #1
starbeam
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0 starbeam is on a distinguished road
Help with a file

I am working on this program that is a hex dump. I have to use a file. I have it where it ask the user what file it is and they enter like test.txt and it opens and uses file test but I want to change it so that it does not ask the user. How would I do that. Here is what I have.
c++ Syntax (Toggle Plain Text)
  1. if ( argc <= 1 )
  2. {
  3. cout << "\n";
  4. cout << "HEXDUMP:\n";
  5. cout << " Please enter the name of a file to be analyzed.\n";
  6.  
  7. cin.getline ( file_in_name, sizeof ( file_in_name ) );
  8.  
  9. handle ( file_in_name );
  10. }
starbeam is offline   Reply With Quote
Old Feb 21st, 2008, 9:35 PM   #2
Ancient Dragon
PFO God In Training
 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 546
Rep Power: 4 Ancient Dragon is on a distinguished road
Re: Help with a file

delete lines 5, 6 and 7 then set file_in_name to whatever you want
file_in_name = "test.txt";
Ancient Dragon is offline   Reply With Quote
Old Feb 21st, 2008, 9:43 PM   #3
starbeam
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0 starbeam is on a distinguished road
Re: Help with a file

Now it is giving me an error.....c:\Documents and Settings\Owner\Desktop\program2\program2.cpp(32) : error C2440: '=' : cannot convert from 'const char [11]' to 'char [80]'

C++ Syntax (Toggle Plain Text)
  1. int main ( long argc, char *argv[] )
  2.  
  3.  
  4. {
  5. char file_in_name[80];
  6. int i;
  7.  
  8.  
  9. if ( argc <= 1 )
  10. {
  11. cout << "\n";
  12. cout << "HEXDUMP:\n";
  13. // cout << " Please enter the name of a file to be analyzed.\n";
  14. file_in_name = "test.txt";
  15. // cin.getline ( file_in_name, sizeof ( file_in_name ) );
  16.  
  17. handle ( file_in_name );
  18. }
starbeam is offline   Reply With Quote
Old Feb 21st, 2008, 10:34 PM   #4
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
Re: Help with a file

This is what Ancient Dragon was telling you to do:

CPP Syntax (Toggle Plain Text)
  1. int main ( long argc, char *argv[] )
  2.  
  3.  
  4. {
  5. char file_in_name[80];
  6. int i;
  7.  
  8.  
  9. if ( argc <= 1 )
  10. {
  11. cout << "\n";
  12. cout << "HEXDUMP:\n";
  13. // cout << " Please enter the name of a file to be analyzed.\n";
  14.  
  15. // cin.getline ( file_in_name, sizeof ( file_in_name ) );
  16.  
  17. handle ( "test.txt" );
  18.  
  19. }
Sane is offline   Reply With Quote
Old Feb 21st, 2008, 10:57 PM   #5
starbeam
Newbie
 
Join Date: Jan 2008
Posts: 10
Rep Power: 0 starbeam is on a distinguished road
Re: Help with a file

thanks that helped
starbeam is offline   Reply With Quote
Old Feb 21st, 2008, 11:27 PM   #6
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Re: Help with a file

In C and C++, you can't assign an array to another array: you have to individually copy each element of the array from one to another. As a C-style string (which is what you're using) is simply an array of characters, you can't do this:
file_in_name = "test.txt";
Instead, you have to do this:
strncpy(file_in_name, "test.txt", 79);
You may have been confused because the first way of doing this is allowed when you declare the variable. It's a quirk of C - you can only do it then.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
problem processing file into a char array csrocker101 C++ 1 May 8th, 2007 11:50 PM
add mutiple users to the smbpasswd file. Pizentios Bash / Shell Scripting 3 Oct 20th, 2005 12:48 PM
After execution - Error cannot locate /Skin File? wchar Visual Basic 1 Mar 5th, 2005 9:04 PM
airport Log program using 3D linked List : problem reading from file gemini_shooter C++ 0 Mar 2nd, 2005 4:12 PM
Structure char field to a disk file ehab_aziz2001 C++ 0 Feb 10th, 2005 2:42 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 3:43 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC