-2

I am curious to know from the more experienced c++ programmers out there if there is a way to have a function read in files that have different formats. Example is one file has a template of house # then street name and the other file is the opposite, house name then street #. Again, just curious if there was some sleek code that I could add to my knowledge and tool box, instead of writing two different inFile functions.

Thank you all for your time.

EDIT

Here is some of the code:

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




using namespace std;

struct WH
{
    int inum;
    string iname;
    string warname;
    int quant;
    float whole;
    float markup;
    float retail;
};


void readinprime(WH);



ifstream inFile;
ofstream outFile;

int main()
{
    WH ware[100];




    //inFile.open("WLL1.txt", ios::in);
    //inFile.open("WLL2.txt", ios::in);
    //inFile.open("WLL3.txt", ios::in);
    //inFile.open("WLL4.txt", ios::in);


    return 0;
}
void readinprime(WH ware[])
{
    int c;
    for(c = 0; c < 100; c++)
    {
        inFile << ware.inum[c] << ware.iname[c];
    }
}

So essentially the first file (WLL1.txt) has the format integer->string and then the next file (WLL2.txt) will have the format string->integer. My question is, is there another way to write the read in function where it can read in int then string & string then int without writing another function? I dont mind writing another function for every file format, but I was just curious if someone had some good tricks that I could add to my tool box. Again thank you for your time.

  • Can you guys us an example? – Kristopher Ives Oct 03 '18 at 03:10
  • where's **your** code? – apple apple Oct 03 '18 at 03:13
  • Sure, we can read any file that fits into memory just as an array of bytes. What you want to do with these raw unstructured bytes? – Öö Tiib Oct 03 '18 at 03:17
  • We can read any file depends on which file do you want to work with. If its custom type then you need your own schema and you need to write the algorithm to read your own schema(sometimes called it as parser). But there are already several schemas available by which you can store your data and read it by just using simple api's. Example:- Json, xml, txt, etc. There are already libraries available to parse all these types of files. – sonulohani Oct 03 '18 at 04:08

1 Answers1

0

you can use the getline method to read every line from the text and store it in a variable

DeCastroAj
  • 30
  • 7