![]() |
|
#1
|
|||
|
|||
C++ printing contents of txt files etcHey guys, I was wondering if someone could maybe give me a hand with this, evidently I lack a bit of understanding
Basically I'm using Visual C++ and *drumrolls* my assignment asks me to design an encryption program. I'm not at the writing a program to carry out encryption using a SSC point yet though, because first I'm asked to read the key in from a text file (the key being plaintext and cipertext, the alphabet with a different alphabet underneath...so A would be W etc). I have to store that file inside a program, in an array called key (it says an array of type char is ideal). Then I have to print of contents of the array on screen, to show it works. So any help would be grand really...I kinda know how to write into files, but I don't know how you print the contents of a file like that, and if there's only a very little difference in code between writing in a file, and reading from it, then I'm afraid I'm a bit lost :-P. OOC: This is my first post btw, so hi all! Though I'm sure this isn't the place for introductions so I'll leave it at that. Expect I'll be hanging around because it seems a pretty interesting place |
|
#2
|
|||
|
|||
|
Reading a file isn't much different from writing a file, and there are even reading equivelents of the writing functions (ie. fputs() = fgets(), fwrite() = fread(), fputs() = fgetc(), etc.). Essentially all you have to do is have a loop that loops until either 1. the end of file is reached or 2. the amount actually read is less than the requested read amount. The latter is usually implemented with fread(), for former more with fgets() and fgetc(). Another option, if you want to read in the entire file, is to use the stat() function to find the size, then allocate memory to accomodate that size. The easiest way to do it with pure ANSI files, though, is to loop until you've encountered an EOF character, like so:
CPP / C++ / C Code:
And if you wanted to store it as an array you could simple have a for() loop and store each retrieved character in the current array index. |
|
#3
|
|||
|
|||
|
That's brilliant! Thank you very much
. |
|
#4
|
|||
|
|||
|
You're using C++. Take advantage of the power of classes instead. Filestreams are IMO a much better way of dealing with file handling.
An ifstream object is needed for getting stuff from files, and an ofstream object for putting information in files, or even an fstream object for doing both. For example, to get stuff from a text file. CPP / C++ / C Code:
All other standard iostream functions also work with filestreams, too... such as getline(), put(), putback(), etc... Your MSDN will have heaps more information about the specifics In my opinion, if you're using C++, don't ignore the I/O stream classes, especially for file handling. Good luck. |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Checking source codes of image, audio and video files | onauc | C Programming Language | 5 | 26-Feb-2005 21:47 |
| converting text files to html files | kensenb | C Programming Language | 13 | 09-Nov-2004 11:33 |
| Apache2 config issues | monev | Apache Web Server Forum | 2 | 28-Jun-2004 06:19 |
| Can't view pages from another machine on the Intranet | aevans | Apache Web Server Forum | 9 | 14-May-2004 02:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The