How to print a String class string to a txt file

Discussion in 'C++' started by Cabomba, Jan 31, 2008.

  1. Cabomba

    Cabomba New Member

    Joined:
    Dec 18, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Hi, I had a problem in printing a String class string to a file where I had declared:

    string str1[10], str2[10];

    And in str1[6] and str2[6] has the values which I wanna store the values by writing them to a txt file using the format:

    // write to apsignal.txt
    newfile= fopen("apsignal.txt","w");
    if (newfile==NULL)
    {
    cout<<"Error opening apsignal.txt";
    fclose(newfile);
    return -1;
    }


    //Print to file with the signal strengths and corresponding clientIDs.
    fprintf(newfile,"1" " %s" " %s", &str1[6], &str2[6] );
    fclose(newfile);

    however, it seems like it cannot print out the values in the txt file as it is a string class? So I was wondering if anyone can give me other alternatives to write the values into this text file?

    Thanks! I had attached the relevant files and pls take a look at the apsignal.txt and you will know what I mean. Thanks for any help!
     

    Attached Files:

  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    Well if you're going to continue to use the C API in a C++ program (I don't recommend it), then it would be

    fprintf(newfile,"1" " %s" " %s", str1[6].c_str(), str2[6].c_str() );
     
  3. Cabomba

    Cabomba New Member

    Joined:
    Dec 18, 2007
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Sorry for the late reply...well...I've followed the way u coded by changing the fprintf to fprintf (newfile, "Test: %s %s\n", str1.c_str(), str2.c_str()); but it doesn't seem to work. Is there anything I need to import or I've left it out?
     
  4. logical_brain

    logical_brain New Member

    Joined:
    Feb 6, 2008
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    using my mind
    Location:
    India
    I Download your attached file but I found nothing in them except some garbage text.
    Anyway
    Check This code

    Code:
    #include <iostream>
    #include <cstdlib>
    #include <cctype>
    #include <fstream>
    #include <cstdio>
    using namespace std;
    
    int main()
    {
        string str[4]={"abc","def","ghi","jkl"};
    /* c++ code
        fstream f;
        f.open("abc.txt",ios::out);
        if(!f)
        {
            cout<<"Cannot Open File";
            return 0;
        }
        f<<str[2];
        f.close();
        f.open("abc.txt",ios::in);
        if(!f)
        {
            cout<<"Cannot Open File";
            return 0;
        }
        f>>str[1];
        cout<<str[1];
        f.close();
    */
    // C Code
        FILE *fp;
        fp = fopen("abc.txt","w");
        if(fp==NULL)
        {
            cout<<"Cannot Open File";
            return 0;
        }
        fprintf(fp,"%s",str[2].c_str());
        fclose(fp);
    	return 0;
    }
    
    For me this code works fine. I think you are using C code in c++. Maybe you are getting this warning if you are using GCC compiler
    This error occurs because
    The %s argument requires a c-styled string, but you've passed it an actual std::string object.
    [/quote]
    Check my above code in c++ code is working fine. Even if you are using %s then use c_str() function to convert it into const char *.



    I am not that much strong in C but amazed why you are using & in fprintf() function.

    You can more read about POD in this link
    http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
    http://www.digitalfanatics.org/index.php?title=CompilerErrors

    Regards
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice