I am writing a string to an excel file a char per cell

This is a discussion on I am writing a string to an excel file a char per cell within the C++ Programming forums, part of the General Programming Boards category; So I ave my readtext1 file which has a bunch of words in sentences. Then I make it so it's ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    229

    I am writing a string to an excel file a char per cell

    So I ave my readtext1 file which has a bunch of words in sentences.

    Then I make it so it's a word per sentence using split_by_sentence.

    Then I feed in a word or line at a time into into the excel_file function.

    Then I split the word into chars by for looping through the word, and passing each char to the excel file cell.

    It works fine but I am only getting the last word, and not the full word but parts of the word.

    I want the entire word in the excel file, not a partial word, and I want every word not just the last word

    Here is my code;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    
    #define MAX_STRING 4096
    
    using namespace std;
    
    void delete_char(char *src, char n, int len)
    {
        char *dst;
        int i;
        // Do not remove NULL characters.
        if ( n == 0 )
            return;
        // Small attempt to control a buffer overflow if the
        // the string is not null-terminated and a proper length
        // is not specified.
        if ( len <= 0 )
            len = MAX_STRING;
        dst = src;
        for ( i = 0; i < len && *src != 0; i++, src++ )
        {
            if ( *src != n )
                *dst++ = *src;
        }
        // Ensure the string is null-terminated.
        *dst = 0;
        return;
    }
    
    void excel_file (std::string str)
    {
        ofstream MyExcelFile("example.csv");
        if(!MyExcelFile.is_open()){
            cout << "The MyExcelFile file is not open" << endl;
        }
    
        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';
        char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
        int i = 0;
        for ( i = 0 ; dels[i] != '\0' ; i++ ) {
            delete_char(writable, dels[i], 0);
        }
        for ( i = 0 ; writable[i] != '\0' ; i++ ) {
    	MyExcelFile << writable[i] << ",";
    	i++;
    	}
        MyExcelFile.close();
    }
    
    void split_by_sentence(void){
        int character, file_character=0;
        char buffer[1024];
        ifstream book ("readtext1.txt");
        if(!book.is_open()){
            cout << "The book file is not open" << endl;
        }
        ofstream book2 ("readtext.txt");
        if(!book2.is_open()){
            cout << "The book2 file is not open" << endl;
        }
        while(file_character!=EOF){
            buffer[0]='\0';
            for(character=0;character<sizeof(buffer);character++)
            {
                file_character=book.get();
                if(file_character==EOF)
                    break;
                if(file_character=='.')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character=='?')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character=='!')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character==' ')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character==',')
                {
                    buffer[character]='\0';
                    break;
                }
                buffer[character]=file_character;
            }
            if(file_character==EOF)
                break;
            book2 << buffer << "." << endl;
        }
        book.close();
        book2.close();
    }
    int main()
    {
        int j = 0;
        vector<string> text_file;
        split_by_sentence();
        ifstream ifs ("readtext.txt");
        string temp;
        if(!ifs.is_open()){
            cout << "The ifs file is not open" << endl;
        }
        while( getline( ifs, temp ) ){
            for (j = 0; temp[j] != '\0'; j++)
                temp[j] = tolower(temp[j]);
            excel_file(temp);
            text_file.push_back( temp );
        }
        ifs.close();
    }
    I'm using codeblocks not visual studio, so there is no functional spaces or tabs format option. So I'm sorry if my code is hard to read.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    By making a small adjustment to the for loop in the excel function I now have the entire last word, not just parts of it.

    Here is the code that I made;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    
    #define MAX_STRING 4096
    
    using namespace std;
    
    void delete_char(char *src, char n, int len)
    {
        char *dst;
        int i;
        // Do not remove NULL characters.
        if ( n == 0 )
            return;
        // Small attempt to control a buffer overflow if the
        // the string is not null-terminated and a proper length
        // is not specified.
        if ( len <= 0 )
            len = MAX_STRING;
        dst = src;
        for ( i = 0; i < len && *src != 0; i++, src++ )
        {
            if ( *src != n )
                *dst++ = *src;
        }
        // Ensure the string is null-terminated.
        *dst = 0;
        return;
    }
    
    void excel_file (std::string str)
    {
        ofstream MyExcelFile("example.csv");
        if(!MyExcelFile.is_open()){
            cout << "The MyExcelFile file is not open" << endl;
        }
    
        char * writable = new char[str.size() + 1];
        std::copy(str.begin(), str.end(), writable);
        writable[str.size()] = '\0';
        char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
        int i = 0;
        for ( i = 0 ; dels[i] != '\0' ; i++ ) {
            delete_char(writable, dels[i], 0);
        }
        for ( i = 0 ; writable[i] != '\0' ; i++ )
        	MyExcelFile << writable[i] << ",";
    
        MyExcelFile.close();
    }
    
    void split_by_sentence(void){
        int character, file_character=0;
        char buffer[1024];
        ifstream book ("readtext1.txt");
        if(!book.is_open()){
            cout << "The book file is not open" << endl;
        }
        ofstream book2 ("readtext.txt");
        if(!book2.is_open()){
            cout << "The book2 file is not open" << endl;
        }
        while(file_character!=EOF){
            buffer[0]='\0';
            for(character=0;character<sizeof(buffer);character++)
            {
                file_character=book.get();
                if(file_character==EOF)
                    break;
                if(file_character=='.')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character=='?')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character=='!')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character==' ')
                {
                    buffer[character]='\0';
                    break;
                }
                if(file_character==',')
                {
                    buffer[character]='\0';
                    break;
                }
                buffer[character]=file_character;
            }
            if(file_character==EOF)
                break;
            book2 << buffer << "." << endl;
        }
        book.close();
        book2.close();
    }
    int main()
    {
        int j = 0;
        vector<string> text_file;
        split_by_sentence();
        ifstream ifs ("readtext.txt");
        string temp;
        if(!ifs.is_open()){
            cout << "The ifs file is not open" << endl;
        }
        while( getline( ifs, temp ) ){
            for (j = 0; temp[j] != '\0'; j++)
                temp[j] = tolower(temp[j]);
            excel_file(temp);
            text_file.push_back( temp );
        }
        ifs.close();
    }
    Last edited by jeremy duncan; 05-11-2012 at 06:58 PM.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here I have fixed the file open bug, so the file has to be open or there is a error message;

    Code:
    #include <iostream>
    #include <string>
    #include <string.h>
    #include <fstream>
    #include <vector>
    
    #define MAX_STRING 4096
    
    using namespace std;
    
    void delete_char(char *src, char n, int len)
    {
        char *dst;
        int i;
        // Do not remove NULL characters.
        if ( n == 0 )
            return;
        // Small attempt to control a buffer overflow if the
        // the string is not null-terminated and a proper length
        // is not specified.
        if ( len <= 0 )
            len = MAX_STRING;
        dst = src;
        for ( i = 0; i < len && *src != 0; i++, src++ )
        {
            if ( *src != n )
                *dst++ = *src;
        }
        // Ensure the string is null-terminated.
        *dst = 0;
        return;
    }
    
    void excel_file (std::string str)
    {
        ofstream MyExcelFile("example.csv");
        if(!MyExcelFile.is_open()){
            cout << "The MyExcelFile file is not open" << endl;
        }
    
        if(MyExcelFile.is_open()){
            char * writable = new char[str.size() + 1];
            std::copy(str.begin(), str.end(), writable);
            writable[str.size()] = '\0';
            char dels[] = ".\n`1234567890-=~!@#$%^&*()_+[]\\{}|;\':\",/<>?$";
            int i = 0;
    
            for ( i = 0 ; dels[i] != '\0' ; i++ ) {
                delete_char(writable, dels[i], 0);
                }
    
            for ( i = 0 ; writable[i] != '\0' ; i++ )
            MyExcelFile << writable[i] << ",";
    
            MyExcelFile.close();
            }
    }
    
    void split_by_sentence(void){
        int character, file_character=0;
        char buffer[1024];
        ifstream book ("readtext1.txt");
        ofstream book2 ("readtext.txt");
        if(!book.is_open()){
            cout << "The book file is not open" << endl;
        }
        if(!book2.is_open()){
            cout << "The book2 file is not open" << endl;
        }
    
        if(book.is_open()){
            if(book2.is_open()){
                while(file_character!=EOF){
                    buffer[0]='\0';
                for(character=0;character<sizeof(buffer);character++){
                    file_character=book.get();
                    if(file_character==EOF)
                    break;
                    if(file_character=='.'){
                        buffer[character]='\0';
                        break;
                        }
                    if(file_character=='?'){
                        buffer[character]='\0';
                        break;
                        }
                    if(file_character=='!'){
                        buffer[character]='\0';
                        break;
                        }
                    if(file_character==' '){
                        buffer[character]='\0';
                        break;
                        }
                    if(file_character==','){
                        buffer[character]='\0';
                        break;
                        }
                    buffer[character]=file_character;
                    }
    
                    if(file_character==EOF)
                    break;
                    book2 << buffer << "." << endl;
                    }
                    book.close();
                    book2.close();
                }
            }
    }
    int main()
    {
        int j = 0;
        vector<string> text_file;
        split_by_sentence();
        string temp;
        ifstream ifs ("readtext.txt");
        if(!ifs.is_open()){
            cout << "The ifs file is not open" << endl;
        }
    
        if(ifs.is_open()){
            while( getline( ifs, temp ) ){
            for (j = 0; temp[j] != '\0'; j++)
                temp[j] = tolower(temp[j]);
            excel_file(temp);
            text_file.push_back( temp );
            }
        ifs.close();
        }
    }

  4. #4
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The whole "design" of this program is quite bad. Think about what you actually want done. Here's how I understand it:

    From a text file, write to another file all the letters separated by commas.

    If that's what you're trying to accomplish (but why?) then it can be done with perhaps a tenth the code you've written.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    The split_by_sentence function opens a text file that has multiple sentences in it.
    Then the sentences are made into a word per line and written to readtext.txt.

    Main then opens readtext and passes a word at a time into the excel_file function.
    The excel file function then takes the word and places each char of the word into it's own cell in the excel cvs file.

    I can get this to work for the last word of the readtext file, but not every word in the readtext file.
    Each word should be on it's own line in the cvs file, in the order it appears in the readtext file.
    Last edited by jeremy duncan; 05-11-2012 at 08:05 PM.

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    As an example of how to make this simpler.
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    int main() {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.txt");
        if (!fin || !fout) { std::cerr<<"file error\n"; std::exit(1); }
        int c;
        bool word = false;
        while ((c = fin.get()) != EOF) {
            if (c == '\'') continue; // skip apostrophe
            if (std::isalpha(c)) {
                if (word) fout<<',';
                fout<<(char)std::tolower(c);
                word = true;
            }
            else if (word) {
                fout<<'\n';
                word = false;
            }
        }
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    That's awesome!

    Here's the code you made but output to a cvs file.

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    int main() {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.csv");
        if (!fin || !fout) { std::cerr<<"file error\n"; std::exit(1); }
        int c;
        bool word = false;
        while ((c = fin.get()) != EOF) {
            if (c == '\'') continue; // skip apostrophe
            if (std::isalpha(c)) {
                if (word) fout<<',';
                fout<<(char)std::tolower(c);
                word = true;
            }
            else if (word) {
                fout<<'\n';
                word = false;
            }
        }
    }
    Thanks.

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I now want to replace the char for a number value.
    So the output has the number value, not the char value.

    Here is the conversion chart.

    alphabetical letter = number value

    a = 466.16
    b = 415.30
    c = 415.30
    d = 415.30
    e = 415.30
    f = 415.30
    g = 349.23
    h = 277.18
    i = 349.23
    j = 440.00
    k = 392.00
    l = 261.63
    m = 261.63
    n = 349.23
    o = 493.88
    p = 261.63
    q = 261.63
    r = 329.63
    s = 311.13
    t = 415.30
    u = 293.66
    v = 415.30
    w = 311.13
    x = 369.99
    y = 392.00
    z = 392.00

  9. #9
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    This works but when I put in a decimal point the fraction doesn't show up in the cvs file?

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    #include <algorithm>
    
    int main() {
        using std::replace;
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.csv");
        if (!fin || !fout) { std::cerr<<"file error\n"; std::exit(1); }
        int c;
        bool word = false;
        while ((c = fin.get()) != EOF) {
            if (c == '\'') continue; // skip apostrophe
            if (std::isalpha(c)) {
                if (word) fout<<',';
    
                if(c == 'a' || c == 'A')
                c = 1;
                else if(c == 'b' || c == 'B')
                c = 1;
                else if(c == 'c' || c == 'C')
                c = 1;
                else if(c == 'd' || c == 'D')
                c = 1;
                else if(c == 'e' || c == 'E')
                c = 1;
                else if(c == 'f' || c == 'F')
                c = 1;
                else if(c == 'g' || c == 'G')
                c = 1;
                else if(c == 'h' || c == 'H')
                c = 1;
                else if(c == 'i' || c == 'I')
                c = 1;
                else if(c == 'j' || c == 'J')
                c = 1;
                else if(c == 'k' || c == 'K')
                c = 1;
                else if(c == 'l' || c == 'L')
                c = 1;
                else if(c == 'm' || c == 'M')
                c = 1;
                else if(c == 'n' || c == 'N')
                c = 1;
                else if(c == 'o' || c == 'O')
                c = 1;
                else if(c == 'p' || c == 'P')
                c = 1;
                else if(c == 'q' || c == 'Q')
                c = 1;
                else if(c == 'r' || c == 'R')
                c = 1;
                else if(c == 's' || c == 'S')
                c = 1;
                else if(c == 't' || c == 'T')
                c = 1;
                else if(c == 'u' || c == 'U')
                c = 1;
                else if(c == 'v' || c == 'V')
                c = 1;
                else if(c == 'w' || c == 'W')
                c = 1;
                else if(c == 'x' || c == 'X')
                c = 1;
                else if(c == 'y' || c == 'Y')
                c = 1;
                else if(c == 'z' || c == 'Z')
                c = 1;
                fout << c;
                word = true;
            }
            else if (word) {
                fout<<'\n';
                word = false;
            }
        }
    }

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    Here is my code with the int values;
    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    int main() {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.csv");
        if (!fin || !fout) { std::cerr<<"file error\n"; std::exit(1); }
        int c;
        bool word = false;
        while ((c = fin.get()) != EOF) {
            if (c == '\'') continue; // skip apostrophe
            if (std::isalpha(c)) {
                if (word) fout<<',';
    
                if(c == 'a' || c == 'A')
                c = 466;
                else if(c == 'b' || c == 'B')
                c = 415;
                else if(c == 'c' || c == 'C')
                c = 415;
                else if(c == 'd' || c == 'D')
                c = 415;
                else if(c == 'e' || c == 'E')
                c = 415;
                else if(c == 'f' || c == 'F')
                c = 415;
                else if(c == 'g' || c == 'G')
                c = 349;
                else if(c == 'h' || c == 'H')
                c = 277;
                else if(c == 'i' || c == 'I')
                c = 349;
                else if(c == 'j' || c == 'J')
                c = 440;
                else if(c == 'k' || c == 'K')
                c = 392;
                else if(c == 'l' || c == 'L')
                c = 261;
                else if(c == 'm' || c == 'M')
                c = 261;
                else if(c == 'n' || c == 'N')
                c = 349;
                else if(c == 'o' || c == 'O')
                c = 493;
                else if(c == 'p' || c == 'P')
                c = 261;
                else if(c == 'q' || c == 'Q')
                c = 261;
                else if(c == 'r' || c == 'R')
                c = 329;
                else if(c == 's' || c == 'S')
                c = 311;
                else if(c == 't' || c == 'T')
                c = 415;
                else if(c == 'u' || c == 'U')
                c = 293;
                else if(c == 'v' || c == 'V')
                c = 415;
                else if(c == 'w' || c == 'W')
                c = 311;
                else if(c == 'x' || c == 'X')
                c = 369;
                else if(c == 'y' || c == 'Y')
                c = 392;
                else if(c == 'z' || c == 'Z')
                c = 392;
                fout << c;
                word = true;
            }
            else if (word) {
                fout<<'\n';
                word = false;
            }
        }
    }

    The input string is converted to a char form, and a word per line.

    Then I give the char a number value, and then place the number value into a csv file.

    So instead of the word in the csv file the number values of the word are in the csv file.



    Here is the float number versions;

    a = 466.16
    b = 415.30
    c = 415.30
    d = 415.30
    e = 415.30
    f = 415.30
    g = 349.23
    h = 277.18
    i = 349.23
    j = 440.00
    k = 392.00
    l = 261.63
    m = 261.63
    n = 349.23
    o = 493.88
    p = 261.63
    q = 261.63
    r = 329.63
    s = 311.13
    t = 415.30
    u = 293.66
    v = 415.30
    w = 311.13
    x = 369.99
    y = 392.00
    z = 392.00



    I just made my work in int form, but I would still like to get the csv output file to have float values instead of int.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    I have fixed my problem with the float result, I just made the int value into float.

    Here is the working code;

    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib> // exit()
    #include <cctype>  // isalpha(), tolower()
    
    int main() {
        std::ifstream fin("file_in.txt");
        std::ofstream fout("file_out.csv");
        if (!fin || !fout) { std::cerr<<"file error\n"; std::exit(1); }
        float c;
        bool word = false;
        while ((c = fin.get()) != EOF) {
            if (c == '\'') continue; // skip apostrophe
            if (std::isalpha(c)) {
                if (word) fout<<',';
    
                if(c == 'a' || c == 'A')
                c = 466.16;
                else if(c == 'b' || c == 'B')
                c = 415.30;
                else if(c == 'c' || c == 'C')
                c = 415.30;
                else if(c == 'd' || c == 'D')
                c = 415.30;
                else if(c == 'e' || c == 'E')
                c = 415.30;
                else if(c == 'f' || c == 'F')
                c = 415.30;
                else if(c == 'g' || c == 'G')
                c = 349.23;
                else if(c == 'h' || c == 'H')
                c = 277.18;
                else if(c == 'i' || c == 'I')
                c = 349.23;
                else if(c == 'j' || c == 'J')
                c = 440;
                else if(c == 'k' || c == 'K')
                c = 392;
                else if(c == 'l' || c == 'L')
                c = 261.63;
                else if(c == 'm' || c == 'M')
                c = 261.63;
                else if(c == 'n' || c == 'N')
                c = 349.23;
                else if(c == 'o' || c == 'O')
                c = 493.88;
                else if(c == 'p' || c == 'P')
                c = 261.63;
                else if(c == 'q' || c == 'Q')
                c = 261.63;
                else if(c == 'r' || c == 'R')
                c = 329.63;
                else if(c == 's' || c == 'S')
                c = 311.13;
                else if(c == 't' || c == 'T')
                c = 415.30;
                else if(c == 'u' || c == 'U')
                c = 293.66;
                else if(c == 'v' || c == 'V')
                c = 415.30;
                else if(c == 'w' || c == 'W')
                c = 311.13;
                else if(c == 'x' || c == 'X')
                c = 369.99;
                else if(c == 'y' || c == 'Y')
                c = 392;
                else if(c == 'z' || c == 'Z')
                c = 392;
                fout << c;
                word = true;
            }
            else if (word) {
                fout<<'\n';
                word = false;
            }
        }
    }
    So no more problem, and thanks again for the code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading/Writing Excel Files
    By milwayh in forum C Programming
    Replies: 4
    Last Post: 11-09-2008, 01:10 PM
  2. Reading/Writing an Excel Spreadsheet?
    By Blackroot in forum Windows Programming
    Replies: 3
    Last Post: 10-15-2007, 10:59 PM
  3. writing char arrays to a file
    By cpluspluser in forum C++ Programming
    Replies: 2
    Last Post: 05-27-2003, 12:17 AM
  4. Writing data to Excel file
    By Hankyaku in forum C++ Programming
    Replies: 3
    Last Post: 04-05-2003, 08:33 AM
  5. how to assign a string into a cell of an array
    By zorro in forum C Programming
    Replies: 4
    Last Post: 04-24-2002, 10:18 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21