Hello,
I want my program to take input from a file rather than from keyboard and also output it to a file not screen. I tried writing prgram. This is what I wrote in visual studio...

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. using namespace std;
  5. void main()
  6. {
  7. freopen("Document.in" ,"r",stdin);
  8. freopen("b","w",stdout);
  9. int x;
  10. cin>>x;
  11. cout<<x<<endl;
  12. getch();
  13. }

But when I run the program nothing happens. I also tried to only take input from file and output it to screen. Then also it gave garbage value.

Recommended Answers

All 12 Replies

didn't get it. whats the problem in my code? It is giving me same garbage value "-858993460".just fed up.

What zeroliken is saying that you use C methods to open a file, C++ methods for in/out stream.

Do you want to open the file with C or C++ code?

And you should ALWAYS close files when you do not need them anymore, not to mention ALWAYS use int main() instead of void main().

I am using cin and cout instead of printf or scanf, so it's C++. And yes I have tried by changing to int main also. It gives garbagge value again. This is my code again...

  1. #include<iostream>
  2. #include<conio.h>
  3. #include<stdio.h>
  4. using namespace std;
  5. void main()
  6. {
  7. freopen("New Text Document.txt" ,"r+",stdin);
  8. freopen("b.txt","w",stdout);
  9. int x;
  10. cin>>x;
  11. cout<<x<<endl;
  12. getch();
    return 0;
  13. }
    

    Output-- "-858993460"

freopen("Document.in" ,"r",stdin);
freopen("b","w",stdout);

is not C++... read the links that zero sent you...

Here is what zero tried to show you:

 ifstream myfile ("example.txt");
 if (myfile.is_open())
 {
    while ( myfile.good() )
    {
      getline (myfile,line);
      cout << line << endl;
    }
    myfile.close();
 }

 else cout << "Unable to open file";

using c++ libraries is a norm but if you would really insist on the code then here's my help...

I tried it myself and got it to work without experiencing garbage values so here's my advice for your problem

  1. make sure that both files can be found at the same directory as the cpp file
  2. make sure that the value inside the "New Text Document.txt" is an integer
  3. conio.h is a deprecated library, it doesn't work for most compilers so I removed it in mine along with getch
  4. always use int main and have a return 0 for a succesful execution of the program
  5. if your still getting a garbage output try to set the value of int x to 0

note: tested the following program with linux's built in g++ compiler

if your still getting a garbage value then could you post what's inside those text file

I really don't know what mistake I am doing.
My code..

include<iostream>
include<stdio.h>

using namespace std;
int main()
{
freopen("New Text Document.txt" ,"r+",stdin);
freopen("bb.txt","w",stdout);
int x;

cin>>x;

cout<<x<<endl;

return 0;
}

OUTPUT still-"-858993460"

text inside new text document---- 1234
bb.txt-- empty

I'll try what you wrote

when i initialised x to zero. and then in next line wrote cin>>x

i got output zero

You reopened (renamed) stdin to be a text file, therefore cin is getting the data from text file intead of keyboard. Check cin for errors. Also check that text file to see that it contains numeric digits, if it doesn't then cin will fail.

text file contains digit. just wrote 1234 in it. it's not taking input from text file.

This worked ok for me. Make sure the input file is in the same folder as the *.exe program


#include <iostream> #include <stdio.h> using namespace std; int main() { FILE* newf = NULL; newf = freopen("TextFile1.txt" ,"r+",stdin); if( newf == NULL) { printf("Can't reopen textfile1.txt\n"); return 1; } newf = freopen("bb.txt","w",stdout); if( newf == NULL) { printf("Can't reopen bb.txt\n"); return 1; } int x; cin>>x; cout << "x = "<<x<<endl; return 0; }

it didn't work. I 'll try writing it Turbo c..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.