CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 1999
    Posts
    1

    Need help Writing/Reading from text file in VC++

    How can i read and write text to and from a text file? I was experimenting with this code, but it places some random text into the text variable and i get a bunch of access violations.

    Here's the code:

    ccode

    #include <iostream>
    #define _AFXDLL
    #include <afx.h>
    using namespace std;

    int main()

    {

    char text[4];
    const int max=10;
    char letter[max];


    CStdioFile file;
    bool bSuccess = file.Open(("c:\_averyunlikelyname.txt"),
    CFile::modeRead |
    CFile::typeText
    );
    if (!bSuccess)
    return FALSE;
    file.ReadString(text,3);

    cout << "The text from the file is: " << text;
    cin >> "press enter to continue";

    return 0;

    }
    /ccode


    any help would be appreciated. thanks.


  2. #2
    Join Date
    May 1999
    Location
    CA, USA
    Posts
    586

    Re: Need help Writing/Reading from text file in VC++

    For an example of reading... check out my Example 16 at http://home.earthlink.net/~railro/mfc_link.html... to write, I'd use CFile::WriteString()

    Rail

    Recording Engineer/Software Developer
    Rail Jon Rogut Software
    [email protected]
    http://home.earthlink.net/~railro/

  3. #3
    Guest

    Re: Need help Writing/Reading from text file in VC++

    what this function do is read words in the file fr0.dic and places the words in four other files fr1.dic, fr2.dic, fr3.dic and fr4.dic(there is 250 words by the file).
    This is the basic read and write function.
    But if your more familiar with <fstream.h> of the regular, you can still use it in visual C++.
    something like
    fstream entree;
    entree.open(filename);
    entree>>my_string;
    or
    entree<<my_string;

    void CBArbre::TranscriptionDictionnaire()
    {
    unsigned char Car = '2';
    char Fichier[17];
    CStdioFile Entree3;
    CStdioFile FichierSortie1;
    CString Dic;
    int Compteur=1,NBMOT=0;

    strset(Fichier,' ');
    strcpy(Fichier,"fr1.dic");

    // Ouverture des fichiers en mode lecture et un autre en mode ecriture
    FichierSortie1.Open(Fichier, CFile::modeCreate | CFile::modeWrite);
    Entree3.Open("fr0.dic", CFile::modeNoTruncate | CFile::modeRead);

    while(1)
    {
    if(!Entree3.ReadString(Dic))
    break;

    FichierSortie1.WriteString(Dic);
    FichierSortie1.WriteString("\n");
    NBMOT++;
    // Si le nombre de mot atteint un multiple de 250
    if(NBMOT == (250*Compteur))
    {
    FichierSortie1.Close(); // Fermer le fichier ecriture courant et reouvrir
    Fichier[2] = Car; // un nouveau fichier en changeant l'indice du
    FichierSortie1.Open(Fichier, CFile::modeCreate | CFile::modeWrite); // fichier(1 a 4)
    Compteur++;
    Car++;
    }

    }
    // fermeture des fichiers de lecture et d'ecriture
    Entree3.Close();
    FichierSortie1.Close();
    } // Fin de la fonction






  4. #4
    Join Date
    Jul 1999
    Posts
    3

    Re: Need help Writing/Reading from text file in VC++

    If you are familiar with MFC, try using the CFile or better yet CStdioFile. Actually, CStdioFile is inherited from CFile. It has a bunch of methods which allow you to open a file in text as well as binary mode, and it has other methods like readstring() and writestring() by which you can write text to a file. Just go thru' the documentation in MFC library reference.



  5. #5
    Guest

    Re: Need help Writing/Reading from text file in VC++

    Just a side note. You may need a \\ instead of a \ for the filename, this may cause some problems later if you try to use directories.
    Rember \ is a special character in strings.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured