Jump to content

C++ ofstream producing chinese text with string


Recommended Posts

The return type of main() is correct in C++ land, but the logic of this programming snippet escapes me. You have odd placements of cin.get() and an infinite loop in omg().

 

You might be seeing "Chinese characters" because the encoding of the output file is not something your text reader is expecting.

 

I do find myself wondering what in the world this program is meant to accomplish.

Link to comment
Share on other sites

Are you trying to learn how to program by reading other people's C++ code? This program is full of issues, most of which aren't even C++ related.

 

I couldn't reproduce your problem on Linux. Everything works fine. I reckon terminating the program (you've got an infinite loop there) while creating ofstream objects pointing to a file might be altering the binary structure of your text file under Windows. Either that or the default encoding of your text editor isn't configured properly.

 

Here's an improved/fixed version of your code: http://pastebin.com/TxB0rF9P

 

If the infinite loop is intentional, uncomment line 10 and comment lines 11 and 14.

 

The return type of main() is correct in C++ land, but the logic of this programming snippet escapes me. You have odd placements of cin.get() and an infinite loop in omg().You might be seeing "Chinese characters" because the encoding of the output file is not something your text reader is expecting.I do find myself wondering what in the world this program is meant to accomplish.

 

He's using cin.get() in main() to prevent the prompt from closing after executing the program outside of cmd. It's not a good practice, but some people like to use it.

Link to comment
Share on other sites

Does it strike anybody else as interesting that there are a disproportionately large number of programmers on this message board? The fact that people can ask programming questions and get answers from several different people is kinda weird, right?

 

It's not that surprising to me. Intellectual ideas are a good bait for people who like dealing with abstractions. Working with abstractions is like 90% of what programmers do. For example, the intellectual side of this community isn't all that difficult for me to understand. I struggle with the real life application of my knowledge. I've also found that my emotional skills still need a lot of development. I think most of us were initially attracted to the intellectual ideas but stayed for the far richer content that Stef is putting out.

Link to comment
Share on other sites

I'm not a C++ guy, but it doesn't make sense to me why you would typecast your main() function to be an integer when it needs to be a string. May be an encoding issue, and you need to cleanse your output in some way?

 

Also, what's with all the "fuck you"s?

main() is ALWAYS int, I don't entirely know why yet, But it is ALWAYS supposed to be int.

Are you trying to learn how to program by reading other people's C++ code? This program is full of issues, most of which aren't even C++ related.

 

I couldn't reproduce your problem on Linux. Everything works fine. I reckon terminating the program (you've got an infinite loop there) while creating ofstream objects pointing to a file might be altering the binary structure of your text file under Windows. Either that or the default encoding of your text editor isn't configured properly.

 

Here's an improved/fixed version of your code: http://pastebin.com/TxB0rF9P

 

If the infinite loop is intentional, uncomment line 10 and comment lines 11 and 14.

 

 

He's using cin.get() in main() to prevent the prompt from closing after executing the program outside of cmd. It's not a good practice, but some people like to use it.

He is aware he has an infinite loop, He did that on purpose. Can you please tell us about the "issues"? We are both learning together.

Does it strike anybody else as interesting that there are a disproportionately large number of programmers on this message board? The fact that people can ask programming questions and get answers from several different people is kinda weird, right?

I don't know why that would be weird, and I've never seen any other programmers on here besides him, And myself.

Link to comment
Share on other sites

main() is ALWAYS int, I don't entirely know why yet, But it is ALWAYS supposed to be int.

 

main() returns an int for both historical and practical reasons. C++ was derived from C, and that was a convention in C. The unix operating system expected that an int value is returned from main(). Actually, it expected some int value to be pushed to the stack as a means of easily composing programs. The next program that was executed at the time your program was closed would be able to read the stack value (or it could be passed to it by the OS) and do something based on that. You could write shell scripts that do particular things based on the value they received from main() (for example, send a message to the system administrator if the program crashed). You could also use the exit information when running other programs from inside your own. It's a good convention, so it became a standard.

 

He is aware he has an infinite loop, He did that on purpose. Can you please tell us about the "issues"? We are both learning together.

 

The issues come from the logical layout of the program, not the correctness of the C++ syntax. Here are the issues I noticed:

  • else if (name != "Lawrence") is redundant. You've already checked for the possibility of the name being Lawrence, so everything else will automatically fall into the else scope.
  • Having global variables (int x; string name;) is a bad practice. I won't go into the reason why because it concerns larger programs than what you have here. You can look it up. It's a big topic.
  • Using bool as a return type for omg() isn't necessary in the context of what you're doing. You're not returning error codes. void is more suitable for these kinds of functions.
  • Putting an infinite loop inside a finite loop is redundant. The program never actually gets to the second iteration of the outer loop.
  • cin.get() in omg() is redundant because of the infinite loop and also because you already have it in main().
  • You were creating a new ofstream instance and opening a file from inside the infinite loop. This is incredibly inefficient.

Compare the code I posted to the one he had before. I recently shared a list of learning resources when it comes to programming. You can find it here.

Link to comment
Share on other sites

This program just seems to consume CPU time and disk space if the expected name is not typed.

 

The file should be closed but the fact that the loop is infinite makes that a mute point.

Also the bell escape sequence, does that do anything?

 

I also don't like the style of multiple statements per line,

I much rather see {} even if redundant and a statement per line.

 

As far as the chinese characters I would open the file in HEX and see if the values match the ASCII equivalent of what you expect. You can open the file with notepad++ and play with the different encoding.

 

Lians, If you are making the name local and passing it to the omg function by reference why not make that reference constant, since the name is not being changed.

 

The endl makes more sense inside the for loop. it seems useless at the end of the file.

Link to comment
Share on other sites

Lians, If you are making the name local and passing it to the omg function by reference why not make that reference constant, since the name is not being changed.

 

The endl makes more sense inside the for loop. it seems useless at the end of the file.

 

You're right, but I figured it's not necessary at this point. Now that I think about it, const correctness might be a good habit to develop early on. As for endl, I tried to keep the original behaviour of his program.

Link to comment
Share on other sites

You're right, but I figured it's not necessary at this point. Now that I think about it, const correctness might be a good habit to develop early on. As for endl, I tried to keep the original behaviour of his program.

 

Yup I agree that good practices even if not necessary should be implemented in order to make them a habit.

Any one else noticed that there's not a single comment? Comments help reveal the intent of the code. Not necessary but if the code needs to be maintained they help.

 

I would also do fuckyou.close() to flush the buffer in your version of the program

Link to comment
Share on other sites

I would also do fuckyou.close() to flush the buffer in your version of the program

 

fstream is a RAII class. It flushes its buffers when instances of the class are destroyed. Adding fuckyou.close() might also give the impression of unsafe memory handling because the code between open() and close() might throw an exception. If you know fstream is a RAII class, that won't be a problem. However, if you're using non-library classes, you have to explicitly check to see if they follow the idiom. In general, I don't manually deallocate resources used by RAII classes.

Link to comment
Share on other sites

fstream is a RAII class. It flushes its buffers when instances of the class are destroyed. Adding fuckyou.close() might also give the impression of unsafe memory handling because the code between open() and close() might throw an exception. If you know fstream is a RAII class, that won't be a problem. However, if you're using non-library classes, you have to explicitly check to see if they follow the idiom. In general, I don't manually deallocate resources used by RAII classes.

 Sure, We might be splitting hairs here.

 

I'm on the camp of explicit de-allocation, Why not free up the resource when you are done? In this code it goes out of scope immediately but when you have tons of lines of code it is nice to know you are done with that variable as soon as you are done with it. It makes it clear to whoever is reading the code. The wrong impression if exceptions occurs seems like a stretch compare to the alternative. but to each their own.

Link to comment
Share on other sites

I would like to hear more from the OP about his thought process, what exactly he was trying to accomplish... and also, what's up with all the "fuck you" stuff?

 

I mean... sure, there are technical people here, but this is first and foremost a philosophy board.  Self-knowledge is a central theme.  You (the OP) could have cleaned up the swearing before posting, but didn't.  What was that about?

Link to comment
Share on other sites

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.