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 04-Nov-2009, 15:27
p8ntballsniper8 p8ntballsniper8 is offline
New Member
 
Join Date: Nov 2009
Posts: 2
p8ntballsniper8 is on a distinguished road

Issues using fstream


So I'm trying to write a program that will read in a character from another file, and then test to see if it's a number, letter, operator, etc etc.

The file I'm getting my input from is a short program that uses a random number generator to output a random character each time it's run. I'm having trouble getting my character variable input into my actual program.

Here's my RNG code: <character.cpp>

CPP / C++ / C Code:
#include <iostream>
#include <ctime>
using namespace std;

int main()
{
srandom(time(NULL));
char x;
x =(random()%128);
cout <<x<<endl;
}

and here's my test program code:

CPP / C++ / C Code:
#include <iostream>
#include <fstream>

using namespace std;

void ShowHeader(); //declaring show header function

int main()
{
ShowHeader(); //initializing header function

ifstream inputStream;

inputStream.open("character.cpp");

if(inputStream.is_open())
{
cout <<"The file is open"<<endl;
inputStream >> x;
cout<<x;
}

x is obviously the variable I'm trying to get from character.cpp, but the cout line for some reason won't output anything, the only output I get is my assignment header and 'The file is open'.

What is it I'm doing wrong? I've never used fstream before so...

Thank you
  #2  
Old 05-Nov-2009, 00:32
Howard_L Howard_L is offline
Regular Member
 
Join Date: Apr 2007
Location: Maryland/PA, USA
Posts: 803
Howard_L is a jewel in the roughHoward_L is a jewel in the roughHoward_L is a jewel in the rough

Re: Issues using fstream


character.cpp only writes one character to the screen (stdout) and NOT to a file, is that what you wanted ?
The second program opens the character.cpp and reads one char of that, not the output x.

Here is an example of setting up a loop to write "count" number of characters to the screen and at the bottom I show how you can redirect that output to a file.
CPP / C++ / C Code:
#include <iostream>  // character.cpp
#include <ctime>
using namespace std;

int main()
{
  srandom(time(NULL));
  char i, x, count = 15;

  for(i = 0; i < count; i++)
  {
    x =(random()%128);
    // cout << (int)x << " ";   // cast x to int to see the ascii code value
    cout << x << " ";           // otherwise this prints the ascii character
  }
  cout << endl;

  return 0;
}
/*
Use > to redirect this output to a file:
  character.exe > character.dat
*/
Next is an example of opening that file and reading one character at a time until EOF is reached.:
CPP / C++ / C Code:
 #include <iostream>
#include <fstream>

using namespace std;

void ShowHeader() { cout << "What the heck is showheader? \n\n"; }  //declaring show header function

int main()
{
  int x;  // must use int size instead of char to "fit" the EOF value.

  ShowHeader(); //initializing header function NOT... YOU'RE "CALLING" IT
 
  ifstream inputStream;

  inputStream.open("character.dat");

  if(inputStream.is_open())
  {
    cout <<"The file is open"<<endl;
    
    while ( (x = inputStream.get()) != EOF)
    {
      cout << (char)x;
    } 
    cout <<  endl;  

    inputStream.close();
  }
  else
  {
    cout << "Problem opening file... " << endl;
  }
  return 0;
}
And here's a sample run:
Code:
~> character.exe > character.dat ~> cat character.dat u g ( E l B 6 v n . 5 . a ~> readchars.exe What the heck is showheader? The file is open u g ( E l B 6 v n . 5 . a
See if that will get you going.
  #3  
Old 09-Nov-2009, 10:28
p8ntballsniper8 p8ntballsniper8 is offline
New Member
 
Join Date: Nov 2009
Posts: 2
p8ntballsniper8 is on a distinguished road

Re: Issues using fstream


Ah, thank you. That worked quite well.

And to answer your question, ShowHeader was a function that was required for me to put it, that was simply three cout commands that put my name, date, etc. at the top of the programs output.
 
 

Recent GIDBlogToyota - 2009 May Promotion by Nihal

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
Is this how our leaders fix nation's most important issues? poppyyo8 Open Discussion Forum 1 24-Mar-2008 10:13
Windows Updates and Secure Website Issues LocalTech Computer Software Forum - Windows 0 31-Jul-2007 05:30
Graphics Issues (Screenshots Included) NovaTiger Computer Hardware Forum 2 03-Apr-2007 18:16
fstream problem Xanathos C++ Forum 3 23-Nov-2006 18:59
Downloadable document compatibilty issues? rhino1616 Web Design Forum 3 07-May-2003 04:05

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:57.


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