Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   Help with a file (http://www.programmingforums.org/showthread.php?t=15230)

starbeam Feb 21st, 2008 10:30 PM

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.
:

  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.   }


Ancient Dragon Feb 21st, 2008 10:35 PM

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";

starbeam Feb 21st, 2008 10:43 PM

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]'

:

  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.   }


Sane Feb 21st, 2008 11:34 PM

Re: Help with a file
 
This is what Ancient Dragon was telling you to do:

:

  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. }


starbeam Feb 21st, 2008 11:57 PM

Re: Help with a file
 
thanks that helped

Ooble Feb 22nd, 2008 12:27 AM

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.


All times are GMT -5. The time now is 4:07 AM.

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