Solved

fstream problem

Posted on 2003-11-28
9
331 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
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
Comment Utility
>>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
Comment Utility
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
Comment Utility
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
 
LVL 1

Expert Comment

by:d_iego
Comment Utility
maybe if you put str= fgets(line,LINESIZE,file) outside the while condition
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

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

Expert Comment

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

0
 
LVL 6

Expert Comment

by:GloomyFriar
Comment Utility
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
Comment Utility
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
Comment Utility
Does the:

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

output show up on stdout?
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
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++.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now