Thread: File Help

  1. #1
    Unregistered
    Guest

    File Help

    Hello

    Why isn't this code working:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h> 
    #include <iostream.h> 
    #include <time.h> 
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	int x=rand()%9;
    	ofstream value("xvalue.txt", ios::ate, filebuf::sh_write );
    	if(!value.is_open())
    	{
    	   cout<<"Could not open file...  Error.... Quitting\n";
    	   system("PAUSE");
    	   return 0;
    	}
        value<<"x="<<x<<"; ";
    	value.close();
    	char yes;
    	cout<<"Would you like to view contents of file (up to 200 characters in)?\n y for yes, anything else for quit\n";
    	cin>>yes;
    	if (yes=='y' || 'Y')
    	{
    		char text[200];
    		ifstream value_read("xvalue.txt", ios::out | ios::nocreate, filebuf::sh_write);
    		if(!value_read.is_open())
    		{
    			cout<<"error, could not open file\n";
    				system("PAUSE");
    			return 0;
    		}
    		    value_read.getline(text, 200, '\n');
    			cout<<text[200]; \\this isn't working
                system("PAUSE");
                value_read.close();
     	}
    	return 0;
    }
    The not working part is it doesn't output the files contents to the screen. And yes, I know there are contents in the file.

  2. #2
    Unregistered
    Guest
    That was me, wasn't logged in. Where you see the smiley face, it should be ios:: out. forget the space, i had to get that in so you could see what it was.

  3. #3
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    I guess I wasn't logged in again. THat was me.

  4. #4
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    ifstream value_read("xvalue.txt", ios::out | ios::nocreate, filebuf::sh_write);

    If you are using ifstream which is used for input, why do you use ios::out which means output to the file. Try removing that. Also, what is filebug::sh_write? I have never seen that before. If you want to open the file for input and output use an fstream instance and give it ios::out and ios::in properties.

  5. #5
    Registered User
    Join Date
    Aug 2001
    Posts
    52
    It's probably working, just not giving you the output you expect.

    cout<<text[200]; \\this isn't working

    your code will output the the contents of text[200] (which, by the
    way, is out of bounds) but not the array's contents. You need to
    loop through the array, input-counter-number-of-times.

    hth,

  6. #6
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Just tried that but my compiler (VC++ .NET) says that getline is not a member of fstream. Then it says bianry '<<': ifstream does not define this.

  7. #7
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    I'll give this a shot...

    Code:
    cout<<text[200];
    will try to print the 200th element of array text. Try just
    Code:
    cout<<text;
    Seems too easy...i might be wrong. try it anyway
    Last edited by Ion Blade; 05-22-2002 at 07:43 PM.
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  8. #8
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469
    Perhaps if I hadn't already tried that. Does anyone want to take the time out of their day to write a program that works to the same effect (give x a random value, write that to a file, display values in file)?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 08:19 PM
  4. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM