bruh
This commit is contained in:
		
							
								
								
									
										98
									
								
								Assignments/filesLab_CalebFontenot/main.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								Assignments/filesLab_CalebFontenot/main.cpp
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
//
 | 
			
		||||
// Created by caleb on 4/8/24.
 | 
			
		||||
//
 | 
			
		||||
#include <fstream>
 | 
			
		||||
#include <iostream>
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 *
 | 
			
		||||
 */
 | 
			
		||||
void test1()
 | 
			
		||||
{
 | 
			
		||||
    std::fstream dataFile;
 | 
			
		||||
 | 
			
		||||
    std::cout << "Opening file....\n";
 | 
			
		||||
    dataFile.open("demofile.txt", std::ios::out);
 | 
			
		||||
    std::cout << "Now writing data to the file.\n";
 | 
			
		||||
    dataFile << "Jones\n";
 | 
			
		||||
    dataFile << "Smith\n";
 | 
			
		||||
    dataFile << "Willis\n";
 | 
			
		||||
    dataFile << "Davis\n";
 | 
			
		||||
    dataFile.close();
 | 
			
		||||
    std::cout << "Done.\n";
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool openFileIn(std::fstream &file, std::string name)
 | 
			
		||||
{
 | 
			
		||||
    bool status;
 | 
			
		||||
    file.open(name, std::ios::in);
 | 
			
		||||
    if (file.fail())
 | 
			
		||||
    {
 | 
			
		||||
        status = false;
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
    {
 | 
			
		||||
        status = true;
 | 
			
		||||
    }
 | 
			
		||||
    return status;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void showContents(std::fstream &file)
 | 
			
		||||
{
 | 
			
		||||
    std::string line;
 | 
			
		||||
 | 
			
		||||
    while (file >> line)
 | 
			
		||||
    {
 | 
			
		||||
        std::cout << line << std::endl;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void test2()
 | 
			
		||||
{
 | 
			
		||||
    std::fstream dataFile;
 | 
			
		||||
 | 
			
		||||
    if (openFileIn(dataFile, "demofile.txt"))
 | 
			
		||||
    {
 | 
			
		||||
        std::cout << "File opened successfully.\n";
 | 
			
		||||
        std::cout << "Now reading the contents of the file.\n\n";
 | 
			
		||||
        showContents(dataFile);
 | 
			
		||||
        dataFile.close();
 | 
			
		||||
        std::cout << "\nDone.\n";
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
        std::cout << "File open error!" << std::endl;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int main()
 | 
			
		||||
{
 | 
			
		||||
    std::string filename;
 | 
			
		||||
    char ch;
 | 
			
		||||
    std::fstream file;
 | 
			
		||||
 | 
			
		||||
    std::cout << "Enter a file name: ";
 | 
			
		||||
    std::cin >> filename;
 | 
			
		||||
 | 
			
		||||
    file.open(filename, std::ios::in);
 | 
			
		||||
 | 
			
		||||
    if(file)
 | 
			
		||||
    {
 | 
			
		||||
        file.get(ch);
 | 
			
		||||
 | 
			
		||||
        while (file)
 | 
			
		||||
        {
 | 
			
		||||
            std::cout << ch;
 | 
			
		||||
 | 
			
		||||
            file.get(ch);
 | 
			
		||||
 | 
			
		||||
        }
 | 
			
		||||
        file.close();
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
    else
 | 
			
		||||
        std::cout << filename << " could not be opened.\n";
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user