Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 344
  • Last Modified:

fstream problem

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
iux
Asked:
iux
  • 3
  • 2
  • 2
  • +2
1 Solution
 
jkrCommented:
>>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
 
jkrCommented:
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
 
iuxAuthor Commented:
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!

 
d_iegoCommented:
maybe if you put str= fgets(line,LINESIZE,file) outside the while condition
0
 
iuxAuthor Commented:
thx,but i think its necessary for this code
0
 
GloomyFriarCommented:
>    if (!strcmp(token,"v")) {
Why "token"? May be "str"?

0
 
GloomyFriarCommented:
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
 
iuxAuthor Commented:
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
 
travdCommented:
Does the:

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

output show up on stdout?
0

Featured Post

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!

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now