GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Mar-2005, 08:21
Venom Venom is offline
New Member
 
Join Date: Mar 2005
Posts: 4
Venom is on a distinguished road

C++ printing contents of txt files etc


Hey 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  
Old 18-Mar-2005, 09:47
Dr. Evil Dr. Evil is offline
Member
 
Join Date: Oct 2004
Location: Netherlands
Posts: 120
Dr. Evil will become famous soon enough
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:
#include <stdio.h>

int main()
{
  FILE *in;
  char c;
  
  in = fopen("filename.txt", "r");
  if(in != NULL)
  {
    while((c = fgetc(in)) != EOF) putchar(c);
    
    fclose(in);
  }
  else printf("Unable to open file");
  
  return 0;
}

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  
Old 18-Mar-2005, 10:02
Venom Venom is offline
New Member
 
Join Date: Mar 2005
Posts: 4
Venom is on a distinguished road
That's brilliant! Thank you very much .
  #4  
Old 18-Mar-2005, 22:12
dexter dexter is offline
Junior Member
 
Join Date: Jul 2004
Location: Queensland, Australia
Posts: 39
dexter will become famous soon enough
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:
#include <iostream>

using std::cout;

#include <fstream>

using std::ios;
using std::ifstream;

int main()
{
  ifstream fin;                   // Initialise filestream object.
  char c;
  
  fin.open("test.txt", ios::in);    // Open an input filestream.
  
  // Check if file opened.
  // fin.fail() returns 1 if there is a fail in the filestream.
  if(fin.fail())
  {
    cout << "Error: Unable to open test.c.\n";
    exit(1);
  }
  
  fin.get(c);                    // Get first character for kicks.
   
  // While the stream hasn't failed or reached the end of file, read and display.
  while(!fin.fail() && !fin.eof())
  {
    cout << c;                 // Display character.
    fin.get(c);                 // Get the next character from the stream.
  }
  
  fin.close();                  // Always close the stream once done.

  return(0);
}

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 GIDBlogProblems with the Navy (Officers) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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
Forum Jump

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

All times are GMT -6. The time now is 10:41.


vBulletin, Copyright © 2000 - 2010, Jelsoft Enterprises Ltd.