File I/O - Newbie Question 
Author Message
 File I/O - Newbie Question

This should be so simple and its not.  I can't open a disk file and write to
it.  I've found some sample code on Microsoft's site and the sample I found
won't compile!  I'm using MS Visual C++ 5.0.

Here's the sample code I found that doesn't work:

    Here's the include stmt:

    #include <fstream.h>

    Here's the two lines where I try to open and write to the file:

    ofstream myFile("c:\\somedirectory\\someFile");
    myFile.put("x");

All I'm trying to do is open the file and write an "x" to it.  I can't even
get this to compile.  Very frustrating.  I get the following errors:

D:\key\SoftKey.c(9) : error C2282: 'ofstream' is followed by 'myFile'
(missing ','?)
D:\key\SoftKey.c(84) : error C2065: 'myFile' : undeclared identifier
D:\key\SoftKey.c(84) : error C2224: left of '.put' must have struct/union
type

The code is straight from Microsoft's knowledgebase.  I've found other
example, and they don't work either.  Maybe I should re-install C++???

Help!!

Thanks,

Doug



Sat, 29 Jun 2002 03:00:00 GMT  
 File I/O - Newbie Question
The first error message:

Quote:
> D:\key\SoftKey.c(9) : error C2282: 'ofstream' is followed by 'myFile'
> (missing ','?)

suggests that there is a missing comma in your code. This might be caused if
the line prior to

Quote:
>     ofstream myFile("c:\\somedirectory\\someFile");

contained something like

int x                // no comma or semicolon

Basically, post more code!

NeilB


Quote:
> This should be so simple and its not.  I can't open a disk file and write
to
> it.  I've found some sample code on Microsoft's site and the sample I
found
> won't compile!  I'm using MS Visual C++ 5.0.

> Here's the sample code I found that doesn't work:

>     Here's the include stmt:

>     #include <fstream.h>

>     Here's the two lines where I try to open and write to the file:

>     ofstream myFile("c:\\somedirectory\\someFile");
>     myFile.put("x");

> All I'm trying to do is open the file and write an "x" to it.  I can't
even
> get this to compile.  Very frustrating.  I get the following errors:

> D:\key\SoftKey.c(9) : error C2282: 'ofstream' is followed by 'myFile'
> (missing ','?)
> D:\key\SoftKey.c(84) : error C2065: 'myFile' : undeclared identifier
> D:\key\SoftKey.c(84) : error C2224: left of '.put' must have struct/union
> type

> The code is straight from Microsoft's knowledgebase.  I've found other
> example, and they don't work either.  Maybe I should re-install C++???

> Help!!

> Thanks,

> Doug




Sat, 29 Jun 2002 03:00:00 GMT  
 File I/O - Newbie Question
I never saw fstream used like that... this is how I would do it (and have
seen it used with VC++)...

     #include <fstream.h>

     Here's the code

     ofstream myFile;
    myFile.open("c:\\somedirectory\\someFile")
    myFile << "x";
    myFile.close();


Quote:
> This should be so simple and its not.  I can't open a disk file and write
to
> it.  I've found some sample code on Microsoft's site and the sample I
found
> won't compile!  I'm using MS Visual C++ 5.0.

> Here's the sample code I found that doesn't work:

>     Here's the include stmt:

>     #include <fstream.h>

>     Here's the two lines where I try to open and write to the file:

>     ofstream myFile("c:\\somedirectory\\someFile");
>     myFile.put("x");

> All I'm trying to do is open the file and write an "x" to it.  I can't
even
> get this to compile.  Very frustrating.  I get the following errors:

> D:\key\SoftKey.c(9) : error C2282: 'ofstream' is followed by 'myFile'
> (missing ','?)
> D:\key\SoftKey.c(84) : error C2065: 'myFile' : undeclared identifier
> D:\key\SoftKey.c(84) : error C2224: left of '.put' must have struct/union
> type

> The code is straight from Microsoft's knowledgebase.  I've found other
> example, and they don't work either.  Maybe I should re-install C++???

> Help!!

> Thanks,

> Doug




Sat, 29 Jun 2002 03:00:00 GMT  
 File I/O - Newbie Question
Thanks Smack'n Rat, but your code has similar problems.  Here's the compile
errors I get with your code (which are lines 11 - 14).  The real problem
seems to be line 11 (ofstream myFile;) . . .

D:\key\SoftKey.c(11) : error C2282: 'ofstream' is followed by 'myFile'
(missing ','?)
D:\key\SoftKey.c(12) : error C2143: syntax error : missing '{' before '.'
D:\key\SoftKey.c(12) : error C2059: syntax error : '.'
D:\key\SoftKey.c(13) : error C2143: syntax error : missing '{' before '<<'
D:\key\SoftKey.c(13) : error C2059: syntax error : '<<'
D:\key\SoftKey.c(14) : error C2143: syntax error : missing '{' before '.'
D:\key\SoftKey.c(14) : error C2059: syntax error : '.'

Could this be a compiler bug or misconfiguration or do I need to try
reinstalling C++??

- Doug


Quote:
> I never saw fstream used like that... this is how I would do it (and have
> seen it used with VC++)...

>      #include <fstream.h>

>      Here's the code

>      ofstream myFile;
>     myFile.open("c:\\somedirectory\\someFile")
>     myFile << "x";
>     myFile.close();



> > This should be so simple and its not.  I can't open a disk file and
write
> to
> > it.  I've found some sample code on Microsoft's site and the sample I
> found
> > won't compile!  I'm using MS Visual C++ 5.0.

> > Here's the sample code I found that doesn't work:

> >     Here's the include stmt:

> >     #include <fstream.h>

> >     Here's the two lines where I try to open and write to the file:

> >     ofstream myFile("c:\\somedirectory\\someFile");
> >     myFile.put("x");

> > All I'm trying to do is open the file and write an "x" to it.  I can't
> even
> > get this to compile.  Very frustrating.  I get the following errors:

> > D:\key\SoftKey.c(9) : error C2282: 'ofstream' is followed by 'myFile'
> > (missing ','?)
> > D:\key\SoftKey.c(84) : error C2065: 'myFile' : undeclared identifier
> > D:\key\SoftKey.c(84) : error C2224: left of '.put' must have
struct/union
> > type

> > The code is straight from Microsoft's knowledgebase.  I've found other
> > example, and they don't work either.  Maybe I should re-install C++???

> > Help!!

> > Thanks,

> > Doug




Sun, 30 Jun 2002 03:00:00 GMT  
 File I/O - Newbie Question

Quote:

>Thanks Smack'n Rat, but your code has similar problems.

There doesn't seem to be anything wrong with the
posted code. As Neil suggested, look at the code
prior to what you posted.


Sun, 30 Jun 2002 03:00:00 GMT  
 File I/O - Newbie Question
I was told by a C++ programmer where I work that the problem was that the
file is a .c and not a .cpp file and that all the code in the dll was C, not
C++.  So I changed to use stdio.h and everthing is fine.

I'm surprised that the compiler didn't give more meaningful information.



Quote:


> >Thanks Smack'n Rat, but your code has similar problems.

> There doesn't seem to be anything wrong with the
> posted code. As Neil suggested, look at the code
> prior to what you posted.



Sun, 30 Jun 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Diff between PPC OS / HPC OS/ eXP OS

2. Newbie: How to discover OS version and available disk space

3. Newbie question : Whats .a files

4. Newbie Question: Borland Compilation (separate files)

5. question related to file (newbie)

6. newbie question re: path to a file

7. Question from a Newbie :: Reading matrices from a file to arrays

8. newbie has question on file processing for C.

9. Newbie question about file io

10. Newbie Question on files

11. creating files---newbie question

12. Newbie Question File Handling

 

 
Powered by phpBB® Forum Software