I/O Stream

This is a discussion on I/O Stream within the C++ Programming forums, part of the General Programming Boards category; I am trying to get this program to open and read from a .txt file. I can see it accessing ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    12

    I/O Stream

    I am trying to get this program to open and read from a .txt file. I can see it accessing the drive, however, it does not appear to be reading it. What am I doing wrong?.

    Thanks.

    Kyle
    Code:
    #include <fstream>
    #include<iostream>
    using std::ifstream;
    using std::ofstream;
    using std::endl;
    using std::cout;
    using std::ios;
    
    int main()
    {
    	
    	ifstream inStream;
    	ofstream outStream;
    
    	inStream.open("A:gradefile.txt");
    	outStream.open("A:outfile.txt");
    
    if(inStream.fail())
    {
    	cout<<"Input file opening failed.\n";
    	exit(1);
    }
    
    	char lastName, firstName, gradeFinal;
    	int quiz, quiz1;
    	int midTerm, final, grade;
    	int prog, prog1;
    	
    	inStream>>lastName>>firstName>>quiz>>quiz1>>midTerm>>final>>prog>>prog1;
    
    	
    	prog=(prog+prog1)/2;
    	quiz=(quiz+quiz1)/2;
    	grade=(prog*.30)+(quiz*.30)+(midTerm*.20)+(final*.20);
    
    	if(grade>=90)
    		gradeFinal='A';
    else if(grade>=80 && grade<90)
    		gradeFinal='B';
    	else if(grade>=70 && grade <80)
    		gradeFinal='C';
    else if(grade>=60 && grade<70)
    		gradeFinal='D';
    	else if(grade<60)
    	gradeFinal='F';
    
    	inStream.close();
    	outStream.close();
    
    	return 0;
    
    }
    Code tags added by Kermi3 - Disabled Smiles too

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    inStream.open("A:gradefile.txt");

    If you're using a Windows operating system, your using the wrong format for your file name. If you navigate to the file on your computer, you'll see A:\ as the directory, and in C++, you have to use \\ to represent a \ because a \ is an escape character. Try this:

    inStream.open("A:\\gradefile.txt");

    If the file was on your A drive in a folder called Data, you would need to do this:

    inStream.open("A:\\Data\\gradefile.txt");
    Last edited by 7stud; 04-06-2003 at 10:54 PM.

  4. #4
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    Yo, dog, where did you get the idea that your program isn't reading from the file?

    You say it appears to be accessing the drive. How would you know that it isn't reading from it? This program has no output, screen or file, as far as I can see. You didn't say anything about it crashing or hanging, so I assume it's running OK. So where's the problem?

    Add some output & it will be fine.

    (I put a little input file on a floppy & your program read it and processed it with no difficulty.)
    Last edited by R.Stiltskin; 04-07-2003 at 12:18 AM.

  5. #5
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77

    Re: I/O Stream

    Originally posted by kylesbigdog
    #include <fstream>
    #include<iostream>
    using std::ifstream;
    using std::ofstream;
    using std::endl;
    using std::cout;
    using std::ios;
    You don't need to specify all of those using's. All you need to do is type: using namespace std; and it will make it so you can use cout, etc without putting the std:: in front of it. For more on namespaces, http://faq.cprogramming.com/cgi-bin/...&id=1043284351

  6. #6
    Lead Moderator kermi3's Avatar
    Join Date
    Aug 1998
    Posts
    2,595

    Code Tags

    I am posting this because you did not use code tags on this thread. In the furture please use Code Tags. They make your code MUCH easier to read and people will be much more likely to help you if you do. And they'll be happier about helping you

    For example:

    Without code tags:

    for(int i=0;i<5;i++)
    {
    cout << "No code tags are bad";
    }

    With Code Tags:
    Code:
    for(int i=0;i<5;i++)
    {
         cout << "This code is easy to read";
    }
    This is of course a basic example...more complicated code is even easier to read with code tags than without.

    I've added code tags for you this time. They can be added by putting [code] at the beginning of your code and [/code] at the end. More information on code tags may be found on the code tag post at the top of every forum. I also suggest you take a look at the board guildlines if you have not done so already.

    This is a common first post mistake, just remember to use [code] tags in the future and you'll get much more help.

    If this is your first time posting here the welcome, and if there's anything I can do or any questions I can answer about these forums, or anything else, please feel free and welcome to PM me.


    Good Luck with your program,

    Kermi3
    Lead Moderator
    Kermi3

    If you're new to the boards, welcome and reading this will help you get started.
    Information on code tags may be found here

    - Sandlot is the highest form of sport.

  7. #7
    Registered User
    Join Date
    Feb 2003
    Posts
    28
    Originally posted by 7stud
    inStream.open("A:gradefile.txt");

    If you're using a Windows operating system, your using the wrong format for your file name. If you navigate to the file on your computer, you'll see A:\ as the directory, and in C++, you have to use \\ to represent a \ because a \ is an escape character. Try this:

    inStream.open("A:\\gradefile.txt");

    If the file was on your A drive in a folder called Data, you would need to do this:

    inStream.open("A:\\Data\\gradefile.txt");
    Greetings,

    Actually
    Code:
    std::ifstream ifs("a:/data/gradefile.txt");
    works just fine in windows.
    Last edited by augur; 04-07-2003 at 09:32 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Raw I/O vs. Stream I/O
    By NuNn in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 08:32 AM
  2. Stream I/O
    By NuNn in forum C Programming
    Replies: 7
    Last Post: 02-23-2009, 01:33 PM
  3. Question: Stream I/O (working with strings)
    By ckuttruff in forum C Programming
    Replies: 15
    Last Post: 05-19-2008, 11:32 AM
  4. Overlapped I/O and Completion Port :: Winsock
    By kuphryn in forum Windows Programming
    Replies: 0
    Last Post: 10-30-2002, 04:14 PM
  5. i/o stream
    By lupi in forum C++ Programming
    Replies: 1
    Last Post: 09-09-2001, 12:55 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21