Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

fstream problem

Posted on 2003-11-28
9
Medium Priority
?
342 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

610 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