0

I'd like to know if there is any way to open a file in C++ using the ifstream class,to read a file with the least "wordy" way (by wordy i mean a lot/redundant commands).I've seen examples where one opens a file for reading,and uses the following concept:

//commands
ifstream fin("data.txt");
char ch;
while(fin){
   fin >> ch;
   cout << ch;
}

Something similar happens while using the getline() function,it would be useful if there was a function where the return value is what was read from istream(stdin or file).

I would appreciate to see some examples with using both C and C++

Thanks for your time!

Ðаn
  • 10,400
  • 11
  • 57
  • 90
zach
  • 43
  • 5
  • What do you mean _"a function where the return value is what was read from istream"_? Do you mean a function to read the whole file? – churill Mar 20 '20 at 15:34
  • No,take a look at the following example:in C,there is the getchar() function.That function returns the character that was read from stdin,so that is handy to use in if or while conditions (mainly to keep reading characters until EOF has been reached). – zach Mar 20 '20 at 15:36
  • 5
    I think there's an element of [XY Problem](http://mywiki.wooledge.org/XyProblem) here. What are you really seeking to do? Why read the file at all if you're not going to use its contents in some way? – Jonathan Leffler Mar 20 '20 at 15:36
  • 2
    So you want `fin.get()`? – KamilCuk Mar 20 '20 at 15:37
  • `std::ifstream fin("data.txt"); std::cout << fin.rdbuf();` ought to work. Not all that useful, though. – user4581301 Mar 20 '20 at 15:39
  • system ("type data.txt"); – armagedescu Mar 20 '20 at 15:40
  • Say you want to print all the characters in the file one by one,so there is no need to actually store the character for further editing. – zach Mar 20 '20 at 15:40
  • @KamilCuk Well yes!That will do! – zach Mar 20 '20 at 15:45
  • Note that `get` doesn't return `char`. Usually it returns an `int` in order to fit in out of band information like EOF. You'll need to add in a cast to make it print the way you expect. – user4581301 Mar 20 '20 at 15:47
  • @user4581301 Yes i noticed,but it's okay. – zach Mar 20 '20 at 15:49
  • This strikes me as a [premature optimization](https://softwareengineering.stackexchange.com/q/80084/356224) problem. If your concern is performance, one stored variable won't make much difference. The overhead of `std::cout` is [*much*](https://stackoverflow.com/q/43051948/8166701) [higher](https://stackoverflow.com/q/38646062/8166701) than that of storing variables. Even if it wasn't, the processor cannot read and write in the same instruction; the processor will read, store data, then write out, regardless of how you express it in C++. – Joshua Wade Mar 20 '20 at 16:10
  • @JoshuaWade It's just that i wanted to see if there is any other way to write the same program but with fewer commands,curiosity. – zach Mar 20 '20 at 16:19
  • @zach Fair enough :) – Joshua Wade Mar 20 '20 at 16:21

2 Answers2

0

If all you want to do is stream the file contents to standard output, you can use a stream iterator like so:

#include <iostream>
#include <iterator>
#include <fstream>
#include <algorithm>
#include <string>

int main( int argc, char **argv )
{
  std::ifstream fin( argv[1] );
  std::copy( std::istreambuf_iterator< char >( fin ),
             std::istreambuf_iterator< char >(),
             std::ostream_iterator< char >( std::cout, "" ) );

  fin.close();
  return 0;
}

EDIT

This version preserves whitespace.


C doesn't provide a good way to do this without creating a variable. The simplest would be something like:

#include <stdio.h>

 int main( int argc, char **argv )
 {
   FILE *in = fopen( argv[1] );
   int ch;
   while ( ( ch = fgetc( in ) ) != EOF )
     putchar( ch );
   fclose( in );
   return 0;
 }
John Bode
  • 106,204
  • 16
  • 103
  • 178
0

The closest match for your code I can think of in C would use fgetc and fputc:

#include <stdio.h>

int main()
{
    FILE* file = fopen("file.txt", "r");
    int ch = fgetc(file);
    while(ch != EOF)
    {
        fputc(ch, stdout);
        ch = fgetc(file);
    }
}

In real code, you would need to do a fair bit of checking that the IO operations went well - e.g. if the fopen succeeded. You may also want to take a look at fgets and fputs.