Click to See Complete Forum and Search --> : Need help Writing/Reading from text file in VC++
distort
July 25th, 1999, 01:06 PM
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.
Rail Jon Rogut
July 25th, 1999, 01:35 PM
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
railro@earthlink.net
http://home.earthlink.net/~railro/
July 26th, 1999, 09:12 AM
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
Hiranmoy Mishra
July 28th, 1999, 11:08 AM
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.
July 28th, 1999, 12:09 PM
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.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.