?
Solved

fstream problem

Posted on 2003-11-28
9
Medium Priority
?
340 Views
Last Modified: 2010-05-18
hallo,
new to c++,i have written a code to write a data ,

  FILE *file;
  file = fopen(fileName,"r");
  ofstream wfile(writefileName);
   
  wfile << "#VRML V2.0 utf8"<<endl;
  wfile << "#Wilson"<<endl;
   
   d = 0;
   while ((str = fgets(line,LINESIZE,file)) && !feof(file))
      {
      if (!strcmp(token,"v")) {

        wfile << "#" << endl;
      cout<<"wfile write ... "<<endl;
      ...........
               }
        }


but after it works,there r only
#VRML V2.0 utf8
#Wilson
in this data

what's happened?
and how can i solve it?

thanks

regards
wilson
0
Comment
Question by:iux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 1000 total points
ID: 9841237
>>what's happened?

You are not verifying that the input file was opened successfully:

file = fopen(fileName,"r");

if (!file) {

  cout << "FAILED to open " << fileName << endl;
}

So, it might just be that the file could not be found.

0
 
LVL 86

Expert Comment

by:jkr
ID: 9841241
btw, it is in general not a good idea to mix C style IO with C++ - why not using an 'ifstream' for the input?
0
 

Author Comment

by:iux
ID: 9841271
i have written
if (!strcmp(token,"v")) {
    cout<<"v read..."
    ....
it works,so i think it opens seccessfully

and i found with sscanf and fscanf much easier than get() and getline for input and ofstream << for output(code easier han fwrite)

its really the problem cause mix?

regards
wilson
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Expert Comment

by:d_iego
ID: 9841347
maybe if you put str= fgets(line,LINESIZE,file) outside the while condition
0
 

Author Comment

by:iux
ID: 9841551
thx,but i think its necessary for this code
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9842197
>    if (!strcmp(token,"v")) {
Why "token"? May be "str"?

0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9842214
And keep in mind that the strcmp() does case-dependent comparison.
It mean that the "#" will be written to out-file when you read exactly "v" from in-file only.
0
 

Author Comment

by:iux
ID: 9842491
i have defined before
char token[1000];


and i have written
if (!strcmp(token,"v")) {
    cout<<"v read..."

it works when compiler
so i thinks it has read "v" successfully
0
 
LVL 1

Expert Comment

by:travd
ID: 9872346
Does the:

cout<<"wfile write ... "<<endl;

output show up on stdout?
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question