-1

So im writing c++ program, that takes a integer from input file, multiply it with 2 and outputs it on output file. So the code is -

#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
    int n;
    FILE * inFile;
    FILE * outFile;
    inFile = fopen ("reiz.in","r");
    outFile = fopen ("reiz.out","r+");
    fscanf (inFile, "%s", n);
    int m = n * 2;
    fprintf (outFile, "%n", n);
    fclose (inFile);
    fclose (outFile);
    return 0;
}

But something is wrong. in reiz.in file there is number 2, after running program it should output 4 in reiz.out, but it just shows don't send error. What exactly is wrong with my script? Best regards, Y2oK

EDIT 1: Ok now it looks like this -

#include <stdio.h>
#include <iostream>
using namespace std;

int main() {
    int n;
    FILE * inFile;
    FILE * outFile;
    inFile = fopen ("reiz.in","r");
    outFile = fopen ("reiz.out","r+");
    fscanf (inFile, "%d", &n);
    int m = n * 2;
    fprintf (outFile, "%d", m);
    fclose (inFile);
    fclose (outFile);
    return 0;
}

but still it gives same don't send error when running reiz.exe file, and it doesn't write anything on output file I'm now a little bit confused, and don't know who to chose as best answer, so I will chose the one who got most "+1". But thanks to all!

Y2ok
  • 171
  • 1
  • 2
  • 10
  • You are still not passing the *address* of `n` to `fscanf()`. Do yourself a favor, use `fstream` instead! – Ferdinand Beyer Oct 25 '11 at 13:38
  • I've edited the question now, so you can copy it complete. – trojanfoe Oct 25 '11 at 13:50
  • 1
    @Y2ok: please use patience and due diligence. You have had good answers. You dismiss them with `I can't compile`, `same don't send` (?), `Still didn't help` and `Same problem :/`. This means you obviously did not try the corrections - correctly. People will run out of patience and then `Still didn't help` will become true: **You** didn't help – sehe Oct 25 '11 at 13:53

6 Answers6

5

This a C-program (apart from the using namespace std;). In C++ you should use streams and formatted I/O, like this:

#include <fstream>

int main() {
    std::ifstream input_file("reiz.in");
    int n;
    input_file >> n; // read one integer
    std::ostream output_file("reis.out");
    output_file << n * 2 << std::endl; // calculate n * 2 and write the result
                                       // to a file. std::endl adds a newline and
                                       // flushes the buffer
    return 0;
}
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
  • I can't compile your program with dev-c++ compiler. – Y2ok Oct 25 '11 at 13:39
  • @Y2ok: Please throw that pile-of-crap compiler away. If you're on Windows, use VisualStudio Express. If on Linux, use GCC. – John Dibling Oct 25 '11 at 13:40
  • Hmm, I am learning c++ from this site - http://www.cplusplus.com/reference/clibrary/cstdio/ , is there maybe site. If that's not c++ what is it? And where can I learn c++ if it's not c++ ? – Y2ok Oct 25 '11 at 13:46
  • @Y2ok: While `cstdio` is valid C++, it is a legacy C interface. C++ provides a new, less error-prone and type-safe solution for working with files: `iostream`. See http://www.cplusplus.com/reference/iostream/ – Ferdinand Beyer Oct 25 '11 at 13:51
  • @Y2ok: That is is OK for a start, but you are reading the part about the C Standard library. If you really want to learn C++, get a [good book](http://stackoverflow.com/questions/388242/). – Björn Pollex Oct 25 '11 at 13:51
  • @Y2ok: You may also check out http://www.cppreference.com/wiki/lv/start, maybe better to read in your own native language. :) – Some programmer dude Oct 25 '11 at 13:52
0

Pass the address of n to fscanf() (and it's an integer not a string):

fscanf (inFile, "%d", &n);

All that stuff is a bit long in the tooth, unless you are actually writing 'C'. Look at the iostream family of classes if you are writing 'C++'

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • You didn't pass the address of `n` did you: `fscanf (inFile, "%d", n);` – trojanfoe Oct 25 '11 at 13:41
  • Hmm, I am learning c++ from this site - cplusplus.com/reference/clibrary/cstdio , is there maybe site. If that's not c++ what is it? And where can I learn c++ if it's not c++ ? – Y2ok Oct 25 '11 at 13:48
0

There are multiple issues with your code:

#include <stdio.h>

This is a C include, not a C++ include. Instead, you should use:

#include <cstdio>

fscanf (inFile, "%s", n);

The format %s is for strings, of type char*. You want to read a decimal number, so use %d instead. Furthermore, you have to pass the address of the destination:

fscanf (inFile, "%d", &n);

fprintf (outFile, "%n", n);

The format %n is used to query the number of bytes written so far, and requires a pointer. Since you want to write a decimal number, use %d again:

fprintf (outFile, "%d", n);
Ferdinand Beyer
  • 64,979
  • 15
  • 154
  • 145
0

Go use iostream (Bjorns answer)

But I think

outFile = fopen ("reiz.out","r+");

should be

outFile = fopen ("reiz.out","w+");

Also

fscanf (inFile, "%d", n);

should be

fscanf (inFile, "%d", &n);

For ease of reference, a fully working (C) program:

#include <stdio.h>

int main() {
    int n;
    FILE * inFile;
    FILE * outFile;
    inFile = fopen ("reiz.in","r");
    outFile = fopen ("reiz.out","w+");
    fscanf (inFile, "%d", &n);
    int m = n * 2;
    fprintf (outFile, "%i", m);
    fclose (inFile);
    fclose (outFile);
    return 0;
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • @Y2ok: you're welcome. Try it (I had a typo earlier, so see whether you missed my edit?) – sehe Oct 25 '11 at 13:44
  • Open mode `"r+"` is fine if the output file exists. If it should be created by the program, it must be `"w"` or `"w+"`. – Ferdinand Beyer Oct 25 '11 at 13:48
  • @FerdinandBeyer: exactly the point. If it doesn't exist, the fprintf fails spectacularly because there is no error handling – sehe Oct 25 '11 at 13:49
  • Thanks, it works, but I am learning c++ from this site - cplusplus.com/reference/clibrary/cstdio . If that's not c++ what is it? And where can I learn c++ if it's not c++ ? – Y2ok Oct 25 '11 at 13:51
  • It says **[`clibrary`](http://cplusplus.com/reference/clibrary/)** right there in the url. Learn c++ [here](http://stackoverflow.com/questions/7890007/c-input-and-output-files/7890086#7890086) or [here](http://cplusplus.com/reference/iostream/) – sehe Oct 25 '11 at 13:55
0

There are two problems with fscanf. First, your format string is wrong -- you're taking a string, where you should be taking a (signed?) decimal. Second, you are passing n by-value, but you should be passing a pointer to n instead:

fscanf (inFile, "%d", &n);

You are also #includeing the now-deprecated <stdio.h> file. You should:

#include <cstdio>

Finally, you claim to be writing C++ code, but the code you actually wrote is decidedly not C++-ish, which is to say it is type-unsafe. I'd recommend you either go C++, or don't -- don't pussyfoot around.

Here is a more C++ way of doing what you're trying to do (you should add error-checking and writing to the output file):

#include <cstdio>
#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream in("reiz.in", std::ios::in);
    int n = 0;
    in >> n;
    return 0;
}
John Dibling
  • 99,718
  • 31
  • 186
  • 324
0

There are several issues which will prevent your code from working properly.

  1. There is no need to open outfile as r+. You can just open it with mode w.

    outFile = fopen ("reiz.out","w");
    
  2. When you read from the file with fscanf, you are passing the integer n instead of the pointer to the integer n. fscanf is expecting to receive the location to write the integer it read. You are passing the integer itself (which will, in the best case, crash your program. Worst case, weird stuff will start happening).

    fscanf (inFile, "%s", &n);
    
  3. You are using %n when writing to outfile. This does not print the integer n but rather it reads the number of bytes written so far. You want to use %d instead.

    fprintf (outFile, "%d", m);
    

Also, as a side-note, you seem to be using C instead of C++. In C++, you would usually use file stream objects, like ifstream and ofstream, to read from and write to files. You are using things like fopen which are decidedly more C-like.

This code should work better:

#include <stdio.h>

int main() {
    int n;
    FILE * inFile;
    FILE * outFile;
    inFile = fopen ("reiz.in","r");
    outFile = fopen ("reiz.out","w");
    fscanf (inFile, "%d", &n);
    int m = n * 2;
    fprintf (outFile, "%d\n", m);
    fclose (inFile);
    fclose (outFile);
    return 0;
}
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77