Visual C++, Read file and Write on another file with modified Help

sh4sh4's Avatar, Join Date: May 2011
Go4Expert Member
Hello Valuable Programmers,

I have a little Problem with Read on a file and Write on another file with modified like;

informations on a exel(.xls) sheet but it doesnt matter we should think like any file type like txt ...;

For Example our xls or txt file have this informations initially;

xxx 16 segment1
xxx 1 segment2
xxx 8 segment3
yyy 70 segment4
zzz 4 segment5
kkk 2 segment6
mmm 1 segment3
mmm 1 segment7
vvv 6 segment3
ppp 1 segment1
qqq 31 segment8
qqq 50 segment2
qqq 62 segment5

i need to do change this informations like this;

xxx total:25 segment1 16,segment2 1,segement3 8
yyy total:70 segment4 70
zzz total:4 segment5 4
kkk total:2 segment6 2
mmm total:2 segment3 1,segment7 1
...

is anyone can make its on C++ its a very important for me,
Please help me
Thank you so much
....
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
Hi have compiled and used on window using Visual studio 2008.

Code:
/**
 * Includes
 */
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
#include <set>

using std::string;

/*
 * Typedefs
 */
typedef std::pair<int, string> intStrpair;
typedef std::multimap<string, intStrpair> MultiMap;
typedef std::map<string, intStrpair> strPiarMap;


int main(int argc, char* argv[])
{
    /**
     * Read the input file and load it's content into a buffer.
     */
    std::fstream dataFile;
    dataFile.open("dataFileIn.txt", std::ios::in | std::ios::binary);
    if(!dataFile)
        return -1;

    dataFile.seekg(0, std::ios::end);
    int  length = dataFile.tellg();
    dataFile.seekg(0, std::ios::beg);
    char *const fileContent  = new char[length];
    dataFile.read(fileContent, length);
    dataFile.close();

    /**
     * Get a single line and put that into a multimap  container.
     */
    bool lastCharIsWhitespace = false;
    bool lastCharIsCR = false;
    int curIndex = 0;
    string word("");
    string key("");
    string value1("");
    string value2("");
    strPiarMap file;

    for(int i = 0; i < length; i++)
    {
        if(isspace(fileContent[i]))
        {
            if(lastCharIsWhitespace == true)
            {
                continue;
            }

            if(curIndex == 0)
            {
                key.assign(word);
                curIndex ++;
            }
            else if(curIndex == 1)
            {
                value1.assign(word);
                curIndex ++;
            }
            else if(curIndex == 2)
            {
                value2.assign(word);
                strPiarMap::iterator findIt;
                value2 = value2.append(" ").append(value1);

                findIt = file.find(key);
                if(findIt != file.end())
                {
                    findIt->second.second.append(",").append(value2);                    
                    findIt->second.first = findIt->second.first + atoi(value1.c_str());
                }
                else
                {
                    intStrpair p(atoi(value1.c_str()), value2);
                    file.insert(std::pair<string, intStrpair>(key, p));
                }

                curIndex = 0;
                key.clear();
                value1.clear();
                value2.clear();
            }
            word.clear();
            lastCharIsWhitespace = true;
        }
        else
        {
            word.append(1, fileContent[i]);
            lastCharIsWhitespace = false;
        }
    }
    delete [] fileContent;

    /**
     * Write back the processed data to file.
     */
    dataFile.open("dataFileOut.txt", std::ios::out);
    if(!dataFile)
        return -1;

    strPiarMap::iterator it;
    for(it= file.begin(); it != file.end(); it++)
    {
        string line("");
        line.append(it->first);
        line.append(" Total:");
        char buf[10] = {'\0'};
        _itoa_s(it->second.first, buf, 10);
        line.append(buf);
        line.append(" ");
        line.append(it->second.second);
        line.append("\n");

        dataFile.write(line.c_str(), line.length());
    }
    dataFile.close();

    return 0;
}
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
output


kkk Total:2 segment6 2
mmm Total:2 segment3 1,segment7 1
ppp Total:1 segment1 1
qqq Total:81 segment8 31,segment2 50
vvv Total:6 segment3 6
xxx Total:25 segment1 16,segment2 1,segment3 8
yyy Total:70 segment4 70
zzz Total:4 segment5 4
0
sh4sh4's Avatar, Join Date: May 2011
Go4Expert Member
Thank you very much my friend
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
>>we should think like any file type like txt

At one level this is true. But there is a huge difference between text files and Excel spreadsheet files, namely the internal formatting of those files. When editing Excel files you need to be careful to preserve the correct formatting otherwise Excel may not be able to read the new file.
0
sh4sh4's Avatar, Join Date: May 2011
Go4Expert Member
Hello dearvivekkumar
You are real c++ expert,thank you.
i have a little another question to you,
my lot of texts are longer that expected i made them short but not enough.
For example:
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 16 segment1
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 1 segment2
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 8 segment3
yyy_ttttttt_hhhhhhhhh_(colcol)_yahaa(sentence)_ 70 segment4
zzz_ttttttt_hhhhhhhhh_(colcol) 4 segment5
kkk_ttttttt_hhhhhhhhh_(colcol) 2 segment6
mmm_ttttttt_hhhhhhhhh_(colcol) 1 segment3
mmm_ttttttt_hhhhhhhhh_(colcol) 1 segment7
vvv_ttttttt_hhhhhhhhh_(colcol)_qq 6 segment3
ppp_ttttttt_hhhhhhhhh_(colcol)_pp 1 segment1
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 31 segment8
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 50 segment2
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 62 segment5

some of lines are ok working fine but in example xxx and yyy lines are dont work because they are too long ,how can i fix or modife this problem?
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
@sh4sh4

Can you bother to write the whole content of your file?
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
@sh4sh4

There was an error in the program which fails only for the last line if the last line does not have the new line character.

For fixing it I have to make some minor changes which I'll write shortly.
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
Code:
/**
Hello Valuable Programmers,

I have a little Problem with Read on a file and Write on another file with modified like;

informations on a exel(.xls) sheet but it doesnt matter we should think like any file type like txt ...;

For Example our xls or txt file have this informations initially;

xxx 16 segment1
xxx 1 segment2
xxx 8 segment3
yyy 70 segment4
zzz 4 segment5
kkk 2 segment6
mmm 1 segment3
mmm 1 segment7
vvv 6 segment3
ppp 1 segment1
qqq 31 segment8
qqq 50 segment2
qqq 62 segment5

i need to do change this informations like this;

xxx total:25 segment1 16,segment2 1,segement3 8
yyy total:70 segment4 70
zzz total:4 segment5 4
kkk total:2 segment6 2
mmm total:2 segment3 1,segment7 1
...

is anyone can make its on C++ its a very important for me,
Please help me
Thank you so much
.... 

**/


/*
Hello dearvivekkumar
You are real c++ expert,thank you.
i have a little another question to you,
my lot of texts are longer that expected i made them short but not enough.
For example:
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 16 segment1
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 1 segment2
xxx_ttttttt_hhhhhhhhh_(sentence)_yahaa(sentence)_ 8 segment3
yyy_ttttttt_hhhhhhhhh_(colcol)_yahaa(sentence)_ 70 segment4
zzz_ttttttt_hhhhhhhhh_(colcol) 4 segment5
kkk_ttttttt_hhhhhhhhh_(colcol) 2 segment6
mmm_ttttttt_hhhhhhhhh_(colcol) 1 segment3
mmm_ttttttt_hhhhhhhhh_(colcol) 1 segment7
vvv_ttttttt_hhhhhhhhh_(colcol)_qq 6 segment3
ppp_ttttttt_hhhhhhhhh_(colcol)_pp 1 segment1
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 31 segment8
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 50 segment2
qqq_ttttttt_hhhhhhhhh_(colcol)_pp 62 segment5

some of lines are ok working fine but in example xxx and yyy lines are dont work because they are too long ,how can i fix or modife this problem? 

*/

/**
 * Includes
 */
#include <iostream>
#include <string>
#include <fstream>
#include <map>
#include <vector>
#include <set>

using std::string;

/*
 * Typedefs
 */
typedef std::pair<int, string> intStrpair;
typedef std::multimap<string, intStrpair> MultiMap;
typedef std::map<string, intStrpair> strPiarMap;


int main(int argc, char* argv[])
{
	/**
	 * Read the input file and load it's content into a buffer.
	 */
	std::fstream dataFile;
	dataFile.open("dataFileIn2.txt", std::ios::in | std::ios::binary);
	if(!dataFile)
		return -1;

	dataFile.seekg(0, std::ios::end);
	int  length = dataFile.tellg();
	dataFile.seekg(0, std::ios::beg);
	char *const fileContent  = new char[length];
	dataFile.read(fileContent, length);
	dataFile.close();

	/**
	 * Get a single line and put that into a multimap  container.
	 */
	bool lastCharIsWhitespace = false;
	bool lastCharIsCR = false;
	int curIndex = 0;
	string word("");
	string key("");
	string value1("");
	string value2("");
	strPiarMap file;
	if(length < 1)
		return -1;

	for(int i = 0; i <= length; i++)
	{
		if((i == length) || isspace(fileContent[i]))
		{
			if(lastCharIsWhitespace == true)
			{
				continue;
			}

			if(curIndex == 0)
			{
				key.assign(word);
				curIndex ++;
			}
			else if(curIndex == 1)
			{
				value1.assign(word);
				curIndex ++;
			}
			else if(curIndex == 2)
			{
				value2.assign(word);
				strPiarMap::iterator findIt;
				value2 = value2.append(" ").append(value1);

				findIt = file.find(key);
				if(findIt != file.end())
				{
					findIt->second.second.append(",").append(value2);					
					findIt->second.first = findIt->second.first + atoi(value1.c_str());
				}
				else
				{
					intStrpair p(atoi(value1.c_str()), value2);
					file.insert(std::pair<string, intStrpair>(key, p));
				}

				curIndex = 0;
				key.clear();
				value1.clear();
				value2.clear();
			}
			word.clear();
			lastCharIsWhitespace = true;
		}
		else
		{
			word.append(1, fileContent[i]);
			lastCharIsWhitespace = false;
		}
	}

	delete [] fileContent;

	/**
	 * Write back the processed data to file.
	 */
	dataFile.open("dataFileOut2.txt", std::ios::out);
	if(!dataFile)
		return -1;

	strPiarMap::iterator it;
	for(it= file.begin(); it != file.end(); it++)
	{
		string line("");
		line.append(it->first);
		line.append(" Total:");
		char buf[10] = {'\0'};
		_itoa_s(it->second.first, buf, 10);
		line.append(buf);
		line.append(" ");
		line.append(it->second.second);
		line.append("\n");

		dataFile.write(line.c_str(), line.length());
	}
	dataFile.close();

	return 0;
}
0
sh4sh4's Avatar, Join Date: May 2011
Go4Expert Member
@dearvivekkumar

Thank you very much again,i solved my problem,the problem was one or two illegal characters like (-,%,&...) in 6000 lines,and turkish characters like (ö,Ö,ü,Ü,ğ,Ğ...),after i cleared these characters program is working excellent,thank you very much again you saved my life i get into debt to you,i want to help you to my friend.

Erkin,