Hello,

I am using turbo c++ 4.0. I know its outdated but i have to use turbo c++ for this project.

Here is my current code,

    #include<iostream.h>
    #include <stdlib.h>
    #include <fstream.h>

    class item
{
private:
    char name[];
    int code[];
    float price[];
    int z;
    fstream datfile;
public:
    void add(void);
    void delete_(void);
    void sell_(void);
    void display_(void);
    void read_(void);
    void initial_(void);
};

void item::initial_(void)
{
     z = 0;
    datfile.open("data.dat",ios::in|ios::out);
}


    void item::read_(void)
    {
        char line;
        //string databk[];
        const int size = 100;
    char* dataArray = new char[size];
        if(datfile.good())
        {

        while(!datfile.eof()){
        for(int i=0; i<size; i++){
           datfile>>dataArray[i];
            if(dataArray[i]=='\n'){
                cout<<"\n";
            }
            cout<<dataArray[i];
        }
        }
        }
        else if(!datfile.good())
        {
            cout<<"Old data file not found or corrupted"<<endl<<"New file will be created this session";
        }
    }

I first coded it with codeblocks 13.12 with mingw compiler. It worked and there was no datfile so it printed the message. And when there was a datfile i used,

   string line;

        while ( getline (datfile,line) )
        {
            cout << line << '\n';

        }

to print those out, and I could store them in array as well.

But in turbo c++, it doesn't work as getline is not available. And i also have no way to check if the file exists or not. I am running turbo c++ with dos box emulator on windows 8 x 64 so i am not sure where the create the data.dat file manually :P it runs on a emulator.

Right now, no matter if the file is there or not, it says its there, i have tried it with if(datafile) and it still says the file exist and prints out,

57a3ef5ff4512f4f399b5f223929da2e
http://i.gyazo.com/0bf403095b646aacbfb977c8bf912b66.png

You can see what it prints out in the first two lines.

I first want to see if it exists and if it does then read it and then store each line in one array which then i need to split (i hope c++ has a split function :P) and send it each to corresponding price, code and name. And i also want to know how to write data to the file as well.
I know how to do it with .net both in vb and c# and i have only learned c++ through game development and still learning it using to use unreal engine 4. I could make it in codeblocks with online docs but i have no idea on how to do it in turbo c++

Recommended Answers

All 3 Replies

BUMP!!

If you only intend to read from the file, then you should only give the ios::in flag. Because the default behavior with ios::out option is to create the file if it does not already exist. Also, you should use ifstream instead of the more general fstream. It's always better to use the most specialized version that meets your needs, this way you always get the correct default behaviors.

I want to read and write on the file, the fist function is to read so I am trying to do it

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.