-4

Regarding the fact tha my previous posts weren't so successful, I'd like to ask a question and I hope that I won't be banned permanently. In C++ for my semester I'll be examined in files along with many other chapters. My question is that supposing I want to handle a text document

Is :

#include <iostream>
#include <fstream>
using namespace std;
int main(){
    ofstream thanosFile; //ofstream allows me to create and WRITE a file
    thanosFile.open("thanos.txt");
    thanosFile << "Hello how are you I'm fine!\n";
    thanosFile.close();
}

same as:

#include "stdafx.h"
#include <stdio.h>
main(){
    FILE f;
    printf("Give name of file"); 
    scanf(%s,thanosFile);
    f=fopen(thanosFile, "w"); // "w" defines that I want to write the file
    scanf("Hello how are you I'm fine!\n");
    fclose(f);
}

?? And furthermore could someone explain me how to handle binary files? Is the approach the same? Can I do it the first way because I find it much easier because I'm getting confused with the formats of the second approach!

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
ThanosA
  • 1
  • 1
  • They're far from the same. For example; the latter one is even more horrible than the former one. –  Feb 08 '15 at 15:18
  • The C++ version (the first one) is needlessly verbose. See [this answer to a related question](http://stackoverflow.com/questions/28253569/what-happens-if-i-never-call-close-on-an-open-file-stream/28253592#28253592). – juanchopanza Feb 08 '15 at 15:19
  • 2
    _`scanf("Hello how are you I'm fine!\n");`_ That doesn't really make sense. You probably meant `fprintf(f,"Hello how are you I'm fine!\n");` – πάντα ῥεῖ Feb 08 '15 at 15:27
  • `f=fopen(thanosFile, "w");` what if `fopen()` fails? – Sourav Ghosh Feb 08 '15 at 15:28
  • You should probably search for binary files in google and learn it yourselves as there is a lot to learn about it. – Arun A S Feb 08 '15 at 15:29
  • If we ignore the "won't compile, but understand what you probably wanted to do" typos in the second case, and the fact that one asks for a filename and the other one doesn't, they are indeed fairly similar. – Mats Petersson Feb 08 '15 at 15:35

1 Answers1

1

I'm very concerned that you don't understand what the code you're writing is doing, and how they're different. You should understand every single line of code that you write.

To answer your question, the first program is written in C++ using streams, and the second one's written in C using the FILE I/O stuff.

The programs are quite similar, except that the first one writes a file in C++ and the second writes to a file in C. So functionally they are the same program, but written in two different languages. (If this is your question)

In terms of writing to binary, I know in the C program, you can give it a "b" in the fopen to set it to binary. To open a binary using ostreams you'd do something like this:

file.open("fiename", ios::binary)

Which opens the file in binary mode. But there is far more to it than this. There's a great website you should read that I can recommend that has something on this C++ File I/O Tutorial

But seriously, I beg you, take the time to understand the code you're writing. Otherwise you're not going to learn anything, you'll notice I've not given you all the answers here, because I'm of the mind that things stick best when you've worked it out for yourself.

I find that I learn the most if I've struggled to get to the answer myself. Sometimes I'll ask on Stack sure, but it'll only be after I've tried everything I can to get the information I need.

Hope this helps