I am running into problems reading in a csv file. I have an inventory.csv file with three columns and around 10,000 rows. The last column is supposed to be ignored. The csv format looks like this
Shelf number; item name; item number. (So here item number shall be ignored).
After reading from user input (user has a choice to enter either the code or item name), I want to display both the shelf number and item name from the inventory csv list. This is for my Uni assignment, and I am not allowed to use any boost or tokeniser classes. Just getline and other basic commands. In addition enumerated type structure with the two values, shelf number and item name, and a structure to build a list (with char * and a pointer to the next element of the list) shall be included. This makes me understand it harder. So far this is how much I understand ,to read one line after a user inputs a line number. Here is the code :

fstream& read_line(fstream& file, unsigned int num)
{
    file.seekg(ios::beg);
    for(unsigned int i=0; i < num - 1; ++i)
        file.ignore(numeric_limits<streamsize>::max(),'\n');

    return file;
}



int main()
{
    fstream file("care_icd10_en.csv",ios_base::in);
    if (!file)
        cout << "Unable to open file \n";
    else
    {
        int line_number;
        cout<< "Please enter a line :"<<endl;
        cin >>line_number;

        read_line(file, line_number);

        if (!file)
            cout << "Wrong line number : " << line_number;
        else
        {
            string line;
            getline(file,line);
            cout <<line<<endl;
            cout << "Search again..";
        }
    }

    return 0;
}

your help is appreciated! Thanks a lot!

Where you read a line of text into the variable "line" (on line 31 of your code, above), don't.

Instead, create a string for storing the shelf number, and a string for storing the item name, and then read in the shelf number ( file >> shelf_number; ) and then read in the item name.

This will give you the values in two different variables. Print them out. Take a look at them. See if they are what you expect, or if you need to do some processing after you've read them in.

Then, instead of making two strings, make a structure containing those strings, and read into the strings inside the structure instead. Check again that everything works. This approach, of building it in stages and checking it works, is very common in programming. It allows you to approach the solution carefully, checking at each stage that things are working.

If it has to be a char* instead of a string, then take out the strings and use a char* instead. Which is just plain stupid but if that's what you have to do, that's what you have to do. If you want to be passive-agressive in your code, first read them into a string, then create a char array of the right size and copy the chars into the array, and add a comment saying it's silly but it's what he instructions say to do. Make sure you have enough space in the char array to store all the letters. If you read into a string first, you can be sure that you do because you can check the size of the string.

Your line-skipping, by the way, is a good idea if you're allowed to only read the line of interest, but the task implies that you're meant to make one structure for every line, and each structure will contain a pointer to the next structure. This is known as a linked list. It is a common data structure.

commented: Thanks a lot #Moschops.My big problem was creating the enum() and defining the struct{}. The instruction goes like this, which I couldn't clearly understand :( " Define a structure to build a list (that is, for two pointers to C strings char * ) and conta +0
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.