Solved

Log information??

Posted on 1998-03-07
18
232 Views
Last Modified: 2008-02-01
Hi I am using Borland C++ Builder v1.0, I would like to know how to have my application put the following in a .dat file (i want it to append--add to the bottom--not overwrite what is in the .dat file).

On <date> at <time> someone entered a wrong password of <password>.

<date> = Current date in short format (i.e 4/14/98)
<time> = Current time in 12 hour format (i.e. 11:37 am)
<password> = Contents of the Edit Box (Edit2)

example
On 4/14/98 at 11:37am someone entered a wrong password of cool.

Then take the statement and append it to badpass.dat (in same directory as my executable application)

thanks
0
Comment
Question by:highvoltagerd
  • 11
  • 7
18 Comments
 

Author Comment

by:highvoltagerd
ID: 1183177
Edited text of question
0
 
LVL 22

Accepted Solution

by:
nietod earned 0 total points
ID: 1183178
Open the file with fopen(), seek to the end (bottom) with fseek(), format and write the information with fprintf(), close the file with fclose().

Read the documentation on these procedures first, and then if you need more help, post a comment here.

0
 

Author Comment

by:highvoltagerd
ID: 1183179
What help file??  I can not find it in any of them..
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183180
These are standard C procedures.  They are documented in any C or C++ reference.  They are in Visual C++ and Borland C++'s online help.  If you don't have access to a basic C++ book or reference, you must get one if you plan on programming in C++.  I use my printed references several times a week!  I use the electronic help many times a day.

What is you level of programming experience?
0
 

Author Comment

by:highvoltagerd
ID: 1183181
I mena Which help file, Builder C++ comes with over 40 help files!
0
 

Author Comment

by:highvoltagerd
ID: 1183182
Well I am a beginner in C++ and an experience programmer In Perl and QBasic
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183183
I can't help you with which help file.  I forgot that Borland's help is less than useless.  (Which is the chief reason I use microsoft's VC++).

If you are an experienced program you should be familiar with file I/O.  The concept is the same in every language/every operating system I ever worked in.  Open the file, seek somewhere, write something and close when you are done.  I'll paste in the docs from VC.  Probably a copyright violation.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183184
fopen, _wfopen

Open a file.

FILE *fopen( const char *filename, const char *mode );

FILE *_wfopen( const wchar_t *filename, const wchar_t *mode );



Function



Required Header



Compatibility





fopen



<stdio.h>



ANSI, Win 95, Win NT



_wfopen



<stdio.h> or <wchar.h>



Win NT




For additional compatibility information, see Compatibility in the Introduction.



Libraries





LIBC.LIB



Single thread static library, retail version



LIBCMT.LIB



Multithread static library, retail version



MSVCRT.LIB



Import library for MSVCRT.DLL, retail version




The c, n, and t mode options are Microsoft extensions for fopen and _fdopen and should not be used where ANSI portability is desired.

Return Value

Each of these functions returns a pointer to the open file. A null pointer value indicates an error.

Parameters

filename Filename

mode Type of access permitted

Remarks

The fopen function opens the file specified by filename. _wfopen is a wide-character version of fopen; the arguments to _wfopen are wide-character strings. _wfopen and fopen behave identically otherwise.



Generic-Text Routine Mappings





TCHAR.H Routine



_UNICODE & _MBCS Not Defined



_MBCS Defined



_UNICODE Defined





_tfopen



fopen



fopen



_wfopen




The character string mode specifies the type of access requested for the file, as follows:

"r" Opens for reading. If the file does not exist or cannot be found, the fopen call fails.

"w" Opens an empty file for writing. If the given file exists, its contents are destroyed.

"a" Opens for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; creates the file first if it doesn't exist.

"r+" Opens for both reading and writing. (The file must exist.)

"w+" Opens an empty file for both reading and writing. If the given file exists, its contents are destroyed.

"a+" Opens for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.

When a file is opened with the "a" or "a+" access type, all write operations occur at the end of the file. The file pointer can be repositioned using fseek or rewind, but is always moved back to the end of the file before any write operation is carried out. Thus, existing data cannot be overwritten.

The "a" mode does not remove the EOF marker before appending to the file. After appending has occurred, the MS-DOS TYPE command only shows data up to the original EOF marker and not any data appended to the file. The "a+" mode does remove the EOF marker before appending to the file. After appending, the MS-DOS TYPE command shows all data in the file. The "a+" mode is required for appending to a stream file that is terminated with the CTRL+Z EOF marker.

When the "r+", "w+", or "a+" access type is specified, both reading and writing are allowed (the file is said to be open for "update"). However, when you switch between reading and writing, there must be an intervening fflush, fsetpos, fseek, or rewind operation. The current position can be specified for the fsetpos or fseek operation, if desired.

In addition to the above values, the following characters can be included in mode to specify the translation mode for newline characters:

t Open in text (translated) mode. In this mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing with "a+", fopen checks for a CTRL+Z at the end of the file and removes it, if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z, may cause fseek to behave improperly near the end of the file.

Also, in text mode, carriage return–linefeed combinations are translated into single linefeeds on input, and linefeed characters are translated to carriage return–linefeed combinations on output. When a Unicode stream-I/O function operates in text mode (the default), the source or destination stream is assumed to be a sequence of multibyte characters. Therefore, the Unicode stream-input functions convert multibyte characters to wide characters (as if by a call to the mbtowc function). For the same reason, the Unicode stream-output functions convert wide characters to multibyte characters (as if by a call to the wctomb function).

b Open in binary (untranslated) mode; translations involving carriage-return and linefeed characters are suppressed.

If t or b is not given in mode, the default translation mode is defined by the global variable _fmode. If t or b is prefixed to the argument, the function fails and returns NULL.

For more information about using text and binary modes in Unicode and multibyte stream-I/O, see Text and Binary Mode File I/O and Unicode Stream I/O in Text and Binary Modes.

c Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called.

n Reset the commit flag for the associated filename to "no-commit." This is the default. It also overrides the global commit flag if you link your program with COMMODE.OBJ. The global commit flag default is "no-commit" unless you explicitly link your program with COMMODE.OBJ.

Valid characters for the mode string used in fopen and _fdopen correspond to oflag arguments used in _open and _sopen, as follows.



Characters in mode String





Equivalent oflag Value for _open/_sopen





a



_O_WRONLY | _O_APPEND (usually _O_WRONLY | _O_CREAT | _O_APPEND)



a+



_O_RDWR | _O_APPEND (usually _O_RDWR | _O_APPEND | _O_CREAT )



r



_O_RDONLY



r+



_O_RDWR



w



_O_WRONLY (usually _O_WRONLY | _O_CREAT | _O_TRUNC)



w+



_O_RDWR (usually _O_RDWR | _O_CREAT | _O_TRUNC)



b



_O_BINARY



t



_O_TEXT



c



None



n



None




Example




/* FOPEN.C: This program opens files named "data"
 * and "data2".It  uses fclose to close "data" and
 * _fcloseall to close all remaining files.
 */

#include <stdio.h>

FILE *stream, *stream2;

void main( void )
{
   int numclosed;

   /* Open for read (will fail if file "data" does not exist) */
   if( (stream  = fopen( "data", "r" )) == NULL )
      printf( "The file 'data' was not opened\n" );
   else
      printf( "The file 'data' was opened\n" );

   /* Open for write */
   if( (stream2 = fopen( "data2", "w+" )) == NULL )
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   /* Close stream */
   if( fclose( stream ) )
      printf( "The file 'data' was not closed\n" );

   /* All other files are closed: */
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}



Output




The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1



Stream I/O Routines

See Also fclose, _fdopen, ferror, _fileno, freopen, _open, _setmode
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183185
fclose, _fcloseall

Closes a stream (fclose) or closes all open streams (_fcloseall).

int fclose( FILE *stream );

int _fcloseall( void );



Function



Required Header



Compatibility





fclose



<stdio.h>



ANSI, Win 95, Win NT



_fcloseall



<stdio.h>



ANSI, Win 95, Win NT




For additional compatibility information, see Compatibility in the Introduction.



Libraries





LIBC.LIB



Single thread static library, retail version



LIBCMT.LIB



Multithread static library, retail version



MSVCRT.LIB



Import library for MSVCRT.DLL, retail version




Return Value

fclose returns 0 if the stream is successfully closed. _fcloseall returns the total number of streams closed. Both functions return EOF to indicate an error.

Parameter

stream Pointer to FILE structure

Remarks

The fclose function closes stream. _fcloseall closes all open streams except stdin, stdout, stderr (and, in MS-DOS®, _stdaux and _stdprn). It also closes and deletes any temporary files created by tmpfile. In both functions, all buffers associated with the stream are flushed prior to closing. System-allocated buffers are released when the stream is closed. Buffers assigned by the user with setbuf and setvbuf are not automatically released.

Example






/* FOPEN.C: This program opens files named "data"
 * and "data2".It  uses fclose to close "data" and
 * _fcloseall to close all remaining files.
 */

#include <stdio.h>

FILE *stream, *stream2;

void main( void )
{
   int numclosed;

   /* Open for read (will fail if file "data" does not exist) */
   if( (stream  = fopen( "data", "r" )) == NULL )
      printf( "The file 'data' was not opened\n" );
   else
      printf( "The file 'data' was opened\n" );

   /* Open for write */
   if( (stream2 = fopen( "data2", "w+" )) == NULL )
      printf( "The file 'data2' was not opened\n" );
   else
      printf( "The file 'data2' was opened\n" );

   /* Close stream */
   if( fclose( stream ) )
      printf( "The file 'data' was not closed\n" );

   /* All other files are closed: */
   numclosed = _fcloseall( );
   printf( "Number of files closed by _fcloseall: %u\n", numclosed );
}



Output




The file 'data' was opened
The file 'data2' was opened
Number of files closed by _fcloseall: 1
 



Stream I/O Routines

See Also _close, _fdopen, fflush, fopen, freopen
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 22

Expert Comment

by:nietod
ID: 1183186
fseek

Moves the file pointer to a specified location.

int fseek( FILE *stream, long offset, int origin );



Function



Required Header



Compatibility





fseek



<stdio.h>



ANSI, Win 95, Win NT




For additional compatibility information, see Compatibility in the Introduction.



Libraries





LIBC.LIB



Single thread static library, retail version



LIBCMT.LIB



Multithread static library, retail version



MSVCRT.LIB



Import library for MSVCRT.DLL, retail version




Return Value

If successful, fseek returns 0. Otherwise, it returns a nonzero value. On devices incapable of seeking, the return value is undefined.

Parameters

stream Pointer to FILE structure

offset Number of bytes from origin

origin Initial position

Remarks

The fseek function moves the file pointer (if any) associated with stream to a new location that is offset bytes from origin. The next operation on the stream takes place at the new location. On a stream open for update, the next operation can be either a read or a write. The argument origin must be one of the following constants, defined in STDIO.H:

SEEK_CUR Current position of file pointer

SEEK_END End of file

SEEK_SET Beginning of file

You can use fseek to reposition the pointer anywhere in a file. The pointer can also be positioned beyond the end of the file. fseek clears the end-of-file indicator and negates the effect of any prior ungetc calls against stream.

When a file is opened for appending data, the current file position is determined by the last I/O operation, not by where the next write would occur. If no I/O operation has yet occurred on a file opened for appending, the file position is the start of the file.

For streams opened in text mode, fseek has limited use, because carriage return–linefeed translations can cause fseek to produce unexpected results. The only fseek operations guaranteed to work on streams opened in text mode are:



•Seeking with an offset of 0 relative to any of the origin values.•Seeking from the beginning of the file with an offset value returned from a call to ftell.



Also in text mode, CTRL+Z is interpreted as an end-of-file character on input. In files opened for reading/writing, fopen and all related routines check for a CTRL+Z at the end of the file and remove it if possible. This is done because using fseek and ftell to move within a file that ends with a CTRL+Z may cause fseek to behave improperly near the end of the file.

Example




/* FSEEK.C: This program opens the file FSEEK.OUT and
 * moves the pointer to the file's beginning.
 */

#include <stdio.h>

void main( void )
{
   FILE *stream;
   char line[81];
   int  result;

   stream = fopen( "fseek.out", "w+" );
   if( stream == NULL )
      printf( "The file fseek.out was not opened\n" );
   else
   {
      fprintf( stream, "The fseek begins here: "
                       "This is the file 'fseek.out'.\n" );
      result = fseek( stream, 23L, SEEK_SET);
      if( result )
         perror( "Fseek failed" );
      else
      {
         printf( "File pointer is set to middle of first line.\n" );
         fgets( line, 80, stream );
         printf( "%s", line );

      }
      fclose( stream );
   }
}



Output




File pointer is set to middle of first line.
This is the file 'fseek.out'.



Stream I/O Routines

See Also ftell, _lseek, rewind
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183187
fwrite

Writes data to a stream.

size_t fwrite( const void *buffer, size_t size, size_t count, FILE *stream );



Function



Required Header



Compatibility





fwrite



<stdio.h>



ANSI, Win 95, Win NT




For additional compatibility information, see Compatibility in the Introduction.



Libraries





LIBC.LIB



Single thread static library, retail version



LIBCMT.LIB



Multithread static library, retail version



MSVCRT.LIB



Import library for MSVCRT.DLL, retail version




Return Value

fwrite returns the number of full items actually written, which may be less than count if an error occurs. Also, if an error occurs, the file-position indicator cannot be determined.

Parameters

buffer Pointer to data to be written

size Item size in bytes

count Maximum number of items to be written

stream Pointer to FILE structure

Remarks

The fwrite function writes up to count items, of size length each, from buffer to the output stream. The file pointer associated with stream (if there is one) is incremented by the number of bytes actually written. If stream is opened in text mode, each carriage return is replaced with a carriage-return – linefeed pair. The replacement has no effect on the return value.

Example






/* FREAD.C: This program opens a file named FREAD.OUT and
 * writes 25 characters to the file. It then tries to open
 * FREAD.OUT and read in 25 characters. If the attempt succeeds,
 * the program displays the number of actual items read.
 */

#include <stdio.h>

void main( void )
{
   FILE *stream;
   char list[30];
   int  i, numread, numwritten;

   /* Open file in text mode: */
   if( (stream = fopen( "fread.out", "w+t" )) != NULL )
   {
      for ( i = 0; i < 25; i++ )
         list[i] = (char)('z' - i);
      /* Write 25 characters to stream */
      numwritten = fwrite( list, sizeof( char ), 25, stream );
      printf( "Wrote %d items\n", numwritten );
      fclose( stream );

   }
   else
      printf( "Problem opening the file\n" );

   if( (stream = fopen( "fread.out", "r+t" )) != NULL )
   {
      /* Attempt to read in 25 characters */
      numread = fread( list, sizeof( char ), 25, stream );
      printf( "Number of items read = %d\n", numread );
      printf( "Contents of buffer = %.25s\n", list );
      fclose( stream );
   }
   else
      printf( "File could not be opened\n" );
}



Output




Wrote 25 items
Number of items read = 25
Contents of buffer = zyxwvutsrqponmlkjihgfedcb



Stream I/O Routines

See Also fread, _write
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183188
fprintf, fwprintf

Print formatted data to a stream.

int fprintf( FILE *stream, const char *format [, argument ]...);

int fwprintf( FILE *stream, const wchar_t *format [, argument ]...);



Function



Required Header



Compatibility





fprintf



<stdio.h>



ANSI, Win 95, Win NT



fwprintf



<stdio.h> or <wchar.h>



ANSI, Win 95, Win NT




For additional compatibility information, see Compatibility in the Introduction.



Libraries





LIBC.LIB



Single thread static library, retail version



LIBCMT.LIB



Multithread static library, retail version



MSVCRT.LIB



Import library for MSVCRT.DLL, retail version




Return Value

fprintf returns the number of bytes written. fwprintf returns the number of wide characters written. Each of these functions returns a negative value instead when an output error occurs.

Parameters

stream Pointer to FILE structure

format Format-control string

argument Optional arguments

Remarks

fprintf formats and prints a series of characters and values to the output stream. Each function argument (if any) is converted and output according to the corresponding format specification in format. For fprintf, the format argument has the same syntax and use that it has in printf.

fwprintf is a wide-character version of fprintf; in fwprintf, format is a wide-character string. These functions behave identically otherwise.



Generic-Text Routine Mappings





TCHAR.H Routine



_UNICODE & _MBCS Not Defined



_MBCS Defined



_UNICODE Defined





_ftprintf



fprintf



fprintf



fwprintf






For more information, see Format Specifcations.

Example




/* FPRINTF.C: This program uses fprintf to format various
 * data and print it to the file named FPRINTF.OUT. It
 * then displays FPRINTF.OUT on the screen using the system
 * function to invoke the operating-system TYPE command.
 */

#include <stdio.h>
#include <process.h>

FILE *stream;

void main( void )
{
   int    i = 10;
   double fp = 1.5;
   char   s[] = "this is a string";
   char   c = '\n';

   stream = fopen( "fprintf.out", "w" );
   fprintf( stream, "%s%c", s, c );
   fprintf( stream, "%d\n", i );
   fprintf( stream, "%f\n", fp );
   fclose( stream );
   system( "type fprintf.out" );
}



Output




this is a string
10
1.500000



Stream I/O Routines

See Also _cprintf, fscanf, sprintf
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183189
Now if you plan to program in Borland's C, you will have to get some reference books!  Bjarne stroustrup has a good one.  There are others.  
0
 

Author Comment

by:highvoltagerd
ID: 1183190
gee.  the help files have confused me even more.  could u please so me some example codes???
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183191
Did you get enough information from the other question?
0
 

Author Comment

by:highvoltagerd
ID: 1183192
nope, i just barly was able to get it to work....
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183193
Its basically the same!

FILE *OutFil = fopen(Path,"a");  // "a" opens for appending so you don't need to fseek().

fprintf(OutFil,"On %i/%i/%i at %i:%i someone entered an incorrect password of %s." Day, Month, Year, Hour, Minute, PswStr);

flose(OutFil);

PswStr is a "char *" that points to the bad password entered.  day, mont, year, hour, minute are integers.

You have to realize that in the future, you will probably not get this much help for free!
0
 

Author Comment

by:highvoltagerd
ID: 1183194
sorry I have no points...
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now