I have this code in C++ but I dont know ho to do it in C. Any Help?

Code #1:

    #include <vector> // for vector
    #include <sstream> // for stringstream
    #include <iostream>
    #include <string>

    int main()
    {
        std::stringstring ss;
        std::string input, temp;
        std::vector<string>; comand;
        std::cout << ": ";
        std::getline(cin, input);
        ss << input;
        // brak up line and put each part into the vector
        while( ss >> temp)
        {
            comand.push_back(temp);
        }
        // display vector
        for(size_t i = 0; i &lt; comand.size(); i++)
            std::cout << comand[i] << std::endl;
        std::cin.get(); // pause program
        return 0;
    }

Code 2:

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

vector <string> split(const string& s, char delim)
{
    vector <string> result;
    istringstream iss(s);
    string part;

    while(getline(iss, part, delim))
        result.push_back(part);

    return result;
}

int main()
{
    ifstream in("test.txt");

    if(in)
    {
        string line;
        string isbn;

        cout<<"\n\n\tEnter ISBN: ";
        cin>>isbn;

        while(getline(in, line))
         {
            vector <string> parts = split(line, ' ');

            if (parts.at(0) == isbn)
            {
                cout<<"Book Number: "<<parts.at(0)<<endl;
                cout<<"Book Title: "<<parts.at(1)<<endl;
                cout<<"Book Author: "<<parts.at(2)<<endl;
            }
        }
    }
}

I think that you would have to use dynamic memory but I dont know how to implement it.

Edited 3 Years Ago by MasterHacker110

You need to learn about pointers, malloc() (for dynamic memory allocation), FILE* (for file i/o), scanf() or fscanf() to replace cin, and printf() to replace cout. Read (google) a C tutorial and you will learn how to do all those things.

I know about the printf() and scanf() functions and know a little about pointers in C. its just the part about splitting the line that i cant seem to get right.

What are you using to do it right now? You can look into the sscanf function and/or the strtok function for this purpose. At a quick glance these will probably be sufficient and you don't have to create a custom solution. Can you post a minimal example of your split function so far? (only a the function itself and a call from main with representative input)

This question has already been answered. Start a new discussion instead.