简体   繁体   中英

C++ file I/O functions

I want to read lines from file input and be able to assign variables to different parts of a line.

Example Input:

4 5.35  Wine Stoppers
2 14.85  Silver Cheese Trays

I'd want a variable for each one of these because I will need to multiply the double in each line by a constant and then send the updated lines to a new file.

I'm not asking for the code, I'm just asking more for the functions that I want to look into to accomplish such a task. This is my first week working in C++ so I assume it's something basic but the teacher didn't teach us what we need to do the lab. He did make me include so I have a feeling I need string functions but I'm having trouble online searching for some that would be useful to me.

What I have so far (buggy):

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

// Accepts: N/A
// Returns: 0 if no error

int main(void){
    ifstream inputFile("order.txt");
    ofstream outputFile("inventory.txt");
    string line;
    char description;
    int quantity, retail;
    const double MARKUP = 2.4;
    while (getline(inputFile, line)){
        inputFile >> quantity >> retail >> description;
        outputFile << quantity << retail * MARKUP << description <<
            endl;
    }
    cout << "Inventory File created" << endl;
    inputFile.close();
    outputFile.close();

    return 0;
}

Check out the iostream module of the standard library, which allows you to work with file streams. In particular, you may want to pay attention to the fstream object which allows you to read files line by line and the stringstream object which allows you to tokenize strings (split them according to a delimiter). Good luck!

You need the stream insertion operator (that is, operator>>() ) or some form of formatted I/O functionality. You can use either, but for our purposes the stream insertion operator is sufficient. For each argument you give to the insertion operator, the stream will go through each character in its buffer and assign the values that correspond to the arguments type. For example:

std::ifstream in("in.txt");

int x;
in >> x;

You should know how this works:

First the streams extracts and discards leading whitespace until it finds the first valid character in the stream. After parsing, if the character is correct for an int , it is extracted into the integer and the stream moves on to the next character. If not, the stream is put into a state of error.

You said that your file looked like this:

 4 5.35 Wine Stoppers 2 14.85 Silver Cheese Trays 

So you will need two integers, two floating-point variables, and four strings for the extraction. You can perform the extraction in the order in which the values appear:

int a, b;
float f1, f2;
std::string wine, silver, trays, stoppers;

in >> a >> f1 >> wine >> stoppers >> b >> f2 >> silver >> cheese >> trays;

If the values appears in a row format in your file, you should use std::getline and loop.

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