Advertisement
aparms

C++ File IO vs C File IO

Jul 25th, 2016
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. // C++ File IO (what I need)
  2.  
  3. #include <cstdlib>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <string>
  7.  
  8. using namespace std;
  9.  
  10. /*
  11.  * Main function.
  12.  */
  13. int main(int argc, char** argv) {
  14.     std::string file_name = argv[1];
  15.     std::ifstream inf(file_name);
  16.     if (!inf) {
  17.         std::cerr << "uhoh cannot read file!" << std::endl;
  18.     }
  19.    
  20.     while (inf) {
  21.         std::string text;
  22.         std::getline(inf, text);
  23.         std::cout << text << std::endl;
  24.     }
  25.    
  26.     return 0;
  27. }
  28.  
  29. // C File IO (what I need)
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <ctype.h>
  33. #include <string.h>
  34.  
  35. /*
  36.  * The main function.
  37.  */
  38. int main(int argc, char** argv) {
  39.    
  40.     if (argc == 2) {
  41.         printf("\nArgument: %s\n", argv[1]);
  42.         FILE *fp = fopen(argv[1], "r");
  43.         if (fp != NULL) {
  44.             char output[50] = "";
  45.             char other_output[] = "Error!";
  46.             puts("Going to print out the data");
  47.             while (!feof(fp)) {
  48.                 fgets(output, 50, fp);
  49.             }
  50.             fclose(fp);
  51.             printf("%s", output);
  52.         }
  53.     } else if (argc > 2) {
  54.         printf("Too many commands supplied.\n");
  55.     } else {
  56.         printf("Not enough commands supplied.\n");
  57.     }
  58.    
  59.     return (EXIT_SUCCESS);
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement