DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C++ (http://www.daniweb.com/forums/forum8.html)
-   -   problems with reading in file (http://www.daniweb.com/forums/thread86164.html)

jaepi Aug 13th, 2007 11:27 pm
problems with reading in file
 
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. :)

jaepi Aug 14th, 2007 12:14 am
Re: problems with reading in file
 
Additional question. Does ifstream reads a space as a beginning of a new line?

Ancient Dragon Aug 14th, 2007 12:18 am
Re: problems with reading in file
 
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
}

Ancient Dragon Aug 14th, 2007 12:19 am
Re: problems with reading in file
 
Quote:

Originally Posted by jaepi (Post 418515)
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.

jaepi Aug 14th, 2007 3:06 am
Re: problems with reading in file
 
Thank you very much sir, it worked.


All times are GMT -4. The time now is 10:57 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC