Solved

fstream problem

Posted on 2003-11-28
9
335 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 500 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
Industry Leaders: 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

739 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