简体   繁体   中英

C++ Program Not Reading Characters from File

So, I'm trying to get the program to read a file and print out the number of sunny, cloudy and rainy days. It just keeps printing 0. What am I missing? I've tried to change the file from a .dat to .txt but still the same result. Here's what's in the data file:

RRCSSSCSCRRRCSSSCSSRSCCRCRRCSS SSSCCSSSCCSSSCCSSSCRCRCCSSSSSS SSSSCSSSCSSSCRRCCCSSSSSCSSSSCS

  #include <iostream> 
    #include <fstream>
    #include <iomanip>
    #include <string>  

    using namespace std;

    int main()

    {  

    const int MONTH = 3;

    const int DAY = 30;

    char name[MONTH][DAY] = {"June", "July", "August"};
    char rain = 'R'; 
    char sun = 'S';    
    char cloud = 'C';    
    char letter;    
    int day = 0;    
    int count = 0;    
    int rainy = 0;    
    int cloudy = 0;
    int sunny = 0;          

    ifstream inputFile;

    cout << " Weather for Summer Months\n";

    cout << "--------------------\n\n";   

    inputFile.open("C:\rainorshine.dat");    

    if (inputFile)
    {
    cout << "Error opening data file.\n";
        system("pause");
    }
    else

    {   cout << "Weather Report\n\n";

    while (inputFile >> letter)
    {
        cout << letter << endl;  // print out characters read from file 
    }             
   for (count = 0; count < MONTH; count++)    
    {    
        for (day = 0; day < DAY; day++)    
        {    
            cout  << name[count][day];    
           inputFile >> name[count][day];    
            if (name[count][day] == 'R')    
                rainy++;
                else if (name[count][day] == 'S')    
                sunny++;            
            else if (name[count][day] == 'C')    
                cloudy++;
        }

        cout << endl;        
        cout << "  Sunny Days Total: " << rainy << endl;    
        cout << "  Rainy Days Total: " << sunny << endl;    
        cout << "  Cloudy Days Total: " << cloudy << endl << endl;      
    }
    system("pause");
        return 0;
    inputFile.close();


    }
    }

This:

while (inputFile >> letter)
{
    cout << letter << endl;  // print out characters read from file 
}  

munches through all the letters, when it's done you're at the end of the file. So when you try to read your data again in the for loop, there's nothing left to read.

"C:\rainorshine.dat"
// ^^

This isn't a backslash followed by a 'r' , it's an escape sequence for carriage return character. You need to use forward slash or escape the backslash itself:

"C:/rainorshine.dat" or "C:\\\\rainorshine.dat"

Which means you never open the file. You would've noticed that if your condition were

if (!inputFile) // notice the !
{
    cout << "Error opening data file.\n";

instead of logically incorrect

if (inputFile) ...

After this line;

while (inputFile >> letter)
{
    cout << letter << endl;  // print out characters read from file 
}

You need to reopen the file or seek back to the beginning. The next time you try to read the file you're already at the end so you get nothing.

Something like;

 inputFile.seekg(0, inputFile.beg);

should solve the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM