Jump to content

So, our programming teacher asked us to make a code for viewing and writing data over a file, it's supposed to be used by a videoclub(yeah, those old things that netflix replaced) and to store films on films.txt on a format ok id,name,year,rating,score,genre . using one line for each movie, using commas to separate each thing, I have to do this also for clients and employees and we have to use the getline function. The part over writing the file is easy, just ask for the data to the user and use 

outfile << id << "," << name << "," << year << ","<< rating << "," << score << "," << genre  << ","; 

What I'm clued in is how may I read the file

Thanks in advanced

#include <iostream>#include <fstream>#include <string>using namespace std;int main(){    ifstream infile ;    ofstream outfile ;    string movies ="movies.txt";    string clients ="Clients.txt";    string employees ="employees.txt"    int optionmenu=0;    while(optionmenu!=7)    {        cout << "Menu:" <<endl;        cout << "1_View employees" <<endl;        cout << "2_View movies" <<endl;        cout << "3_View clients" <<endl;        cout << "4_New employee" <<endl;        cout << "5_New movie" <<endl;        cout << "6_New client" <<endl;        cout << "7_Salir" <<endl;        cin  >> optionmenu;        switch(optionmenu)        {        case 1:            infile.open("employees.txt")            if (infile.is_open)            {            }        case 2:            infile.open("movies.txt")            if (infile.is_open())            {                            }        case 3:            infile.open("clients.txt")            if (infile.is_open())            {                            }        }            return 0;}

Case: NZXT phantom CPU:I5-4460 GPU:MSI-GTX1070 Gaming X RAM:2x4Gb-DDR3-HyperX fury MOBO:Asus Z97-P HDD:Toshiba 1Tb 7200rpm PSU:Sentey650W

Link to comment
https://linustechtips.com/topic/477542-c-file-reading/
Share on other sites

Link to post
Share on other sites

well I wont give you the answer because you would learn little from it, but what you want to reasearch is "string split" which will help you divide each line of text by the commas and "file read line" which will take one line at a time from a file using a while loop

 

so your file will look like

 

id,name,year,rating,score,genre

id,name,year,rating,score,genre

id, etc...

 

and your program will go line by line, and split the line by the commas, then take each word that is between the commas

hope that helps

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to comment
https://linustechtips.com/topic/477542-c-file-reading/#findComment-6398205
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×