Thread: File Reading Errors

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    . Driveway's Avatar
    Join Date
    May 2002
    Posts
    469

    Question File Reading Errors

    When I run this code:


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <fstream.h> 
    #include <iostream.h> 
    #include <time.h> 
    #include <string.h>
    
    
    int main()
    {
    	srand((unsigned)time(NULL));
    	int x=rand()%9;
    	ofstream value("xvalue.txt", ios::ate | ios::in, filebuf::sh_write );
    	if(!value.is_open())
    	{
    	   cout<<"Could not open file...  Error.... Quitting\n";
    	   system("PAUSE");
    	   return 0;
    	}
    	value<<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' || yes== 'Y')
    	{
    		char text[200];
    		ifstream read_value("xvalue.txt", ios::out | ios::nocreate);
    		if (!read_value.is_open())
    		{
    			printf("Error opening file\n");
    			system("PAUSE");
    			return 0; 
    		}
            read_value>>text;
    		for (int i=0; i<=200; i++)
    		printf("%c", text[i]);
    		printf("\n\n");
    		read_value.close();
            system("PAUSE");	
    		printf("\n\nWould you like to delete the contents of the file?");
    		cin>>yes;
            if (yes== 'y' || yes=='Y')
           read_value.clear(0);
     	}
    	return 0;
    }
    I get what I want, but if I have any spaces in the file it stops reading there. Also, if I read a file it reads what's in it, and then adds some various characters. How can I fix both of these problems?
    Last edited by Driveway; 05-24-2002 at 03:40 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM