Hello there, I have here a program that reads a txt file taking each word in three columns separated by a comma and storing it to a vector. It has a filter that whenever it sees a comma, it seperates the word from each other. My problem is, whenever I type a phrase or a sentence in a line, the space is considered to a be a beginning of a new line in which ruining the output and producing a run time error. Here's my code and my input txt file.

CODE:

#include <iostream>
#include <vector>
#include <cctype>
#include <string>
#include <fstream>
#include <iomanip>
#include <stdlib.h>
#include <windows.h>


using namespace std;


vector<string> split_String(const string& str) //reading csv file
{
    vector<string> vStr;
    int i = 0;
    while (i != str.size()) 
    {
        while (i != str.size() && str[i]==',' ){
            i++;
        }    
        
        int j = i;
        while (j != str.size() && str[j]!=',' ){
            
            j++;
        }
    
        if (i != j) 
        {
            vStr.push_back(str.substr(i, j - i));
            i = j;
         }

    }

    return vStr;

}


int main() 
{
    vector<string> vCode;
    vector<string> vWord;
    vector<string> vFile;
    std::string my_File("sample.txt");
    ifstream inFile(my_File.c_str());
    std::string str1;
    const char *pStorage = 0;
    int arr = 0;

    if (!inFile) //if file cannot be read
    {
        cout << "Unable to open file";
        exit(1); 
    }
    else
    {
        while(!inFile.eof()) // reading the file line by line
        {                    
            
            inFile >> str1;
            pStorage = str1.data();
            cout << "BEFORE = " << pStorage << endl;
            vector<string> vOutput = split_String(pStorage);
            

            for (int i = 0; i < vOutput.size(); i++)
            {
                if(i == 0)
                {
                    vCode.push_back(vOutput[i]);
                    cout << "CODE = ";
                    
                }
                else if(i == 1)
                {
                    vWord.push_back(vOutput[i]);
                    cout << "WORD = ";
                    
                }
                else if(i == 2)
                {
                    vFile.push_back(vOutput[i]);
                    cout << "FILE = ";
                    
                } 

                    
            }            
            cout << endl;
            
        }
        
        
    }
    inFile.close();

    ofstream outfile("output.bat");

    for(int x =0; x < vCode.size(); x++)
    {
        outfile<<"sed -e 's/";
        outfile<<vCode[x];
        outfile<<"/";
        outfile<<vWord[x];
        outfile<<"/g' ";
        outfile<<vFile[x];
        outfile<<" > english/";
        outfile<<vFile[x];
        outfile<<"\n";



    }


    outfile.close();
    

    system("output.bat");

    return 0;

    

}

TEXT FILE INPUT:

STR-000,Login Name,login.html,
STR-001,Password,login.html,
STR-029,Login,login.html,
STR-030,Help,login.html,
STR-031,Version,login.html,

Notice the first line in the text file input where there is a space between the words Login and Name. Whenever my program reads it, it considers it as a new line, destroying the entire output. I've tried filtering the space by considering it as a char but it still produces the same output. What do you think should I do? Thank you. :)

Additional question. Does ifstream reads a space as a beginning of a new line?

If you use getline() on line 64 you will avoid the space problem. getline() will read the entire line, then you can separate it into comma-separated tokens.

Line 61 is incorrect -- you should avoid using eof() like that because it will cause problems. Use getline() instead, something like this

while( getline(str,inFile) )
{
    // blabla
}

Additional question. Does ifstream reads a space as a beginning of a new line?

No -- when you use the extraction operator >> the stream only reads up to the first space. That is why I suggest getline() in my previous post.

Thank you very much sir, it worked.

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.