|
try this:
FILE *fp;
fp = fopen(filename, "r");
ASSERT(fp!=NULL);
char * buffer[MAX_LINE_SIZE];
fgets(buffer, MAX_LINE_SIZE, fp);
// here, buffer contains one line from your file.
|
|
|
|
|
CStdioFile f;
if (f.Open(filename, CFile::modeRead)==0)
{
...error
}
CString csText;
if (!f.ReadString(csText))
{
....error
}
Smaller Animals Software, Inc.
You're the icing - on the cake - on the table - at my wake. Modest Mouse
|
|
|
|
|
#include <string>
#include <vector>
#include <algorithm>
#include <fstream>
#include <iostream>
#include <sstream>
using std::copy;
using std::vector;
using std::string;
using std::ifstream;
using std::ostream_iterator;
using std::cout;
using std::back_inserter;
using std::getline;
using std::ostringstream;
using std::istream_iterator;
int main(int argc, char* argv[])
{
string currentLine;
ifstream str("c:\\winzip.log");
if (!str.is_open())
{
cout << "It's stuffed\n";
return -1;
}
vector<string> file_contents;
copy(istream_iterator<string>(str),istream_iterator<string>(), back_inserter(file_contents));
// Now print out the results
copy(file_contents.begin(), file_contents.end(), ostream_iterator<string>(cout, "\n"));
return 0;
}
This example reads a file in and prints the output to the console. However, the pertinent point is that you can create an fstream, which is bidirectional, or just an ifstream or ofstream, and then if you know the file format, you can, for example, do this:
myFile >> nMyNumber >> szMyString >> etc.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Hey! The guy is coming from VB. Are you going to make him afraid of C++?
I vote pro drink
|
|
|
|
|
|
MDR ?
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
MDR == Mort De Rire in French visit Irc Channel
|
|
|
|
|
I donno what that means but apparently people would call you a "damn STL MDR" if you start pushing STL down VB coders' throats
Nish
p.s. What's MDR anyway???
Sonork ID 100.9786 voidmain
www.busterboy.org
If you don't find me on CP, I'll be at Bob's HungOut
|
|
|
|
|
Nemanja Trifunovic wrote:
Hey! The guy is coming from VB. Are you going to make him afraid of C++?
VB and C I thought he said. Admittedly the sample code is more complex than it needed to be in order to make the point ( it's a copy & paste from an earlier question ), but from memory the MFC classes for file IO use the shift operators as well, so why not point someone to the proper way to do things ?
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Christian Graus wrote:
VB and C I thought he said.
Yes, he said, but if he really knew C, he would have simply used the lucy's solution (fopen).
I vote pro drink
|
|
|
|
|
Nemanja Trifunovic wrote:
Yes, he said, but if he really knew C, he would have simply used the lucy's solution (fopen).
Actually, you're right, and that had occured to me when I read lucy's response, but then I saw Chris' MFC reliant post, so I felt obliged to defend the standard (which IMO equates to 'proper') way of doing things.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
CG
You sure are an STL warrior warring against the ruling MFCians.
Nish
Sonork ID 100.9786 voidmain
www.busterboy.org
If you don't find me on CP, I'll be at Bob's HungOut
|
|
|
|
|
Nish [BusterBoy] wrote:
You sure are an STL warrior warring against the ruling MFCians.
And proud of it. Bring them on.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Look up CFile in MSDN.
Click here and use the menu on the left side to navigate thought the members functions etc.
Sprudling
|
|
|
|
|
What does CFile do that IOStreams do not ?
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
What does IOStreams do that fopen etc doesn't?
Tim Smith
Descartes Systems Sciences, Inc.
|
|
|
|
|
I'm not sure, but let me hazard some guesses.
IOStreams are C++ based, and a file stream will always close the file in it's destructor, even if an exception is raised, as soon is it goes out of scope.
IOStreams can be extended to allow one step reading/writing of custom file types, as well as custom modifiers that need only be written once and work with every type to be streamed.
Once the code is written, output can be streamed to any other stream type ( the console, a custom stream type, etc. ), simply by changing the type of the stream, all the other code will remain valid.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
I just thought that since he's using MFC he might as well use CFile.
Sprudling
|
|
|
|
|
I just thought that since he's using MFC he might as well use CFile.
Fair enough. I thought so long as he's reading a file he may as well learn the best way to do it. I was asking seriously, because I didn't know if there actually is some reason one might choose CFile beyond sticking with MFC.
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|
Thanks for all the help. I knew it should not be that hard.
|
|
|
|
|
Here would be the basics to read a file in C++:
#include < fstream.h >
void main()
{
//File Stream Object
fstream DataFile;
char FileName[81];
cout << "Enter the name of the file you wish to open\n";
cin.getline(FileName, 81);
DataFile.open(FileName, ios::out);
}
Nick Parker
|
|
|
|
|
i don't understand why the insertitem function is good and the text doesn't appear!!
void CMyListView::OnInitialUpdate()
{
CListView::OnInitialUpdate();
ModifyStyle(0, LVS_REPORT,0);
// this code only works for a report-mode list view
//ASSERT(GetStyle() & LVS_REPORT);
// Gain a reference to the list control itself
CListCtrl& theCtrl = GetListCtrl();
// Insert a column. This override is the most convenient.
theCtrl.InsertColumn(0, _T("Names"), LVCFMT_CENTER);
// The other InsertColumn() override requires an initialized
// LVCOLUMN structure.
LVCOLUMN col;
col.mask = LVCF_FMT | LVCF_TEXT;
col.pszText = _T("Version");
col.fmt = LVCFMT_CENTER;
theCtrl.InsertColumn(1, &col);
theCtrl.InsertColumn(2,"Description",LVCFMT_CENTER);
// Set reasonable widths for our columns
theCtrl.SetColumnWidth(0, 200);
theCtrl.SetColumnWidth(1, 100);
theCtrl.SetColumnWidth(2, 100);
GetListCtrl().InsertItem(0, "First row");
}
GetListCtrl().InsertItem(0, "First row") is good but the text doesn't appear!!
Help me, please
PS: LVS_TYPEMASK == LVS_LIST only Michael Dunn. It is not that
|
|
|
|
|
|
I use this code in my view class (according to Christian Graus articles about GDI+)
using namespace Gdiplus;
Graphics graphics(pDC->m_hDC);
Pen pen(Color(255,234,0,123));
int y = 0;
for (int x = 0; x <= 250; x += 30)
{
graphics.DrawLine(&pen, x, 0, 0, y);
graphics.DrawLine(&pen, x, 250, 250, y);
graphics.DrawLine(&pen, y, 0, 250, x);
graphics.DrawLine(&pen, y, 250, 0, x);
y += 30;
}
When I run the application nothing appear in client area,but after I minimiza and maximiza framework then I can see lines
Does anybody know the reason?
Thanks
Mazy
Don't Marry a Person You Can Live With...
Marry Someone You Can Not Live Without
|
|
|
|
|
Where are you doing the drawing ?
Christian
I have come to clean zee pooollll. - Michael Martin Dec 30, 2001
Picture the daffodil. And while you do that, I'll be over here going through your stuff.
|
|
|
|
|