Problems writing to a file

This is a discussion on Problems writing to a file within the C++ Programming forums, part of the General Programming Boards category; I exported an access database to a text file and ensured that each line is of the form: ###############AAAAAAAAAAAAAAAAAAAAAA(variable length ...

  1. #1
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90

    Question Problems writing to a file

    I exported an access database to a text file and ensured that each line is of the form:
    ###############AAAAAAAAAAAAAAAAAAAAAA(variable length comment)

    I read in the whole line into whole, and split whole into header (#'s) and comment (A's). From there I try to break up the comment into 70 byte fields and write a record of the form HeaderComment70 (I place header and comment2 into whole and write whole). But it's not writing anything. What did I do wrong?
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ifstream in;
        in.open("Remark.prn");
        if (!in)
        {
            cout<<"Error opening Remark.prn\n";
            return 1;
        }
        ofstream out;
        out.open("Remark1.prn");
        if(!out)
        {
            cout<<"Error opening Remark1.prn\n";
            return 1;
        }
        
        char header[16]="";
        char comment[9970];
        char comment2[99870];
        char whole[10000]="";
        int place=0;
        int seq=0;
        char foo;
        
        while(!in.eof())
        {
            in.getline(whole,sizeof(whole));
            
            for(int x=0;x<sizeof(header);x++)
            {
                header[x]=whole[x];
                place=x;
            }
            for(int x=place;whole[x]!='\n';x++)
                comment[place-x]=whole[x];
            /*header and comment now separated*/
            for(int x=0;x<sizeof(whole);x++)
                whole[x]=' ';
            place=0;
            
            
            for(int x=0;foo!='\n';x++)
            {
                foo=comment[x];
                comment2[x-place]=foo;
                if(x%69==0)
                {
                    strcat(whole,header);
                    switch(x)
                    {
                        case 69: strcat(whole,"01");
                            break;
                        case 138: strcat(whole,"02");
                            break;
                        case 207: strcat(whole,"03");
                            break;
                        case 276: strcat(whole,"04");
                            break;
                        case 245: strcat(whole,"05");
                            break;
                        case 414: strcat(whole,"06");
                            break;
                        case 483: strcat(whole,"07");
                            break;
                        case 552: strcat(whole,"08");
                            break;
                        case 621: strcat(whole,"09");
                            break;
                        case 690: strcat(whole,"10");
                            break;
                        case 759: strcat(whole,"11");
                            break;
                        case 828: strcat(whole,"12");
                            break;
                        case 897: strcat(whole,"13");
                            break;
                        case 966: strcat(whole,"14");
                            break;
                    }
                    strcat(whole,comment2);
                    strcat(whole,"\n");
                    place=x;
                    for(int y=0;x<sizeof(whole);y++)
                    {
                        out<<whole[y];
                        whole[y]=' ';
                    }
                    for(int y=0;y<sizeof(comment2);y++)
                        comment2[y]=' ';
                }
            }
            
        }
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > for(int x=place;whole[x]!='\n';x++)
    After calling getline(), whole doesn't have a newline at the end, but it does have a string terminator ('\0'), so change the above to:
    Code:
            for(int x=place; whole[x]!='\0'; x++)
    And you would also change this one:
    > for(int x=0;foo!='\n';x++)

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Code:
    char header[16]="";
    char comment[9970];
    char comment2[99870];
    char whole[10000]="";
    Jeesh... your compiler doesn't complain about 120,000 bytes on the local stack? This is the C++ board, think about maybe using the string container class instead of all those character arrays.


    Code:
    for(int x=place;whole[x]!='\n';x++)
            comment[place-x]=whole[x];
    So (in addition to what Swoopy said), place and x start out at 15. Then comment[0] = whole[15] which is OK. Then we increment x so it becomes 16. Then we try to say comment[-1] = whole[16]... Do you see the problem there?

    Study this:
    Code:
    #include <string>
    #include <sstream>
    #include <iomanip>
    #include <iostream>
    using namespace std;
    
    
    int main()
    {
        string data, header;
        int count = 1;
        char buffer[3];
    
        cout << "Enter some text:" << endl;
        getline(cin,data);
    
        // Get first 2 characters into string header
        header = data.substr(0,2);
    
        // Put remaining characters into stringstream sstr
        stringstream sstr(data.substr(2));
    
        // Continuously extract 2 characters from stringstream until no more
        while( sstr.get(buffer,sizeof(buffer)) )
            cout << header << setfill('0') << setw(2) << count++
                 << string(buffer) << endl;
    
        return 0;
    }
    My Input/Output:
    Code:
    Enter some text:
    hello there my friend
    he01ll
    he02o 
    he03th
    he04er
    he05e 
    he06my
    he07 f
    he08ri
    he09en
    he10d
    Now adapt/apply that to your needs.
    Last edited by hk_mp5kpdw; 02-09-2006 at 09:12 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Slime Dragoon_42's Avatar
    Join Date
    Feb 2003
    Location
    Vancouver
    Posts
    90
    Here's the less stupid version of my code after the help (Thanks everyone!!). This one actually works too.
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    #include <sstream>
    using namespace std;
    
    int main()
    {
        ifstream in;
        in.open("Remark.prn");
        if (!in)
        {
            cout<<"Error opening Remark.prn\n";
            return 1;
        }
        ofstream out;
        out.open("Remark1.prn");
        if(!out)
        {
            cout<<"Error opening Remark1.prn\n";
            return 1;
        }
        
        string whole, header, comment;
        int count=1;
        char buffer[70];
        
        while(!in.eof())
        {
            getline(in,whole);
            header=whole.substr(0,15);
            stringstream sstr(whole.substr(15));
            
            while(sstr.get(buffer,sizeof(buffer)))
                out<<header<<count++<<string(buffer)<<endl;
            count=1;
        }
        return 0;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,340
    >> while(!in.eof())

    While I doubt it will affect the accuracy of your program in this case, you should get in the habit of avoiding using eof() to control your loop. In this case, simply moving the getline call to the while control would work as well if not better: while (getline(in,whole)).

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,803
    Not that it matters much:
    Code:
    string whole, header, comment;
    comment isn't being used and so this can be removed.
    Code:
    while(sstr.get(buffer,sizeof(buffer)))
        out<<header<<count++<<string(buffer)<<endl;
    I think you should be able to just say buffer instead of string(buffer) since the sstr.get line will store up to sizeof(buffer)-1 characters and then null terminate the result so it should be safe to just output buffer all by itself.

    [edit]
    Also, since you are not using setw or setfill in your code, you can drop the #include <iomanip> header.
    [/edit]
    Last edited by hk_mp5kpdw; 02-09-2006 at 10:46 AM.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. File Writing Problem
    By polskash in forum C Programming
    Replies: 3
    Last Post: 02-13-2009, 09:47 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

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