-2

This code was for implementing cat in Lin, and when I compiled it, it returned me "tp undefined". When I asked my professor, he said the way I used getline is in C language. I'm confused.

int main(int argc, char*argv[]) {
  FILE* file;
  for (int i = 1; i < argc; i++) {
    file = fopen(argv[i],"w");//

    if (file < 0) {
      perror("Error, Can't open file!");
      return -1;
    }

    if (file.is_open()) {
      string tp;
      while (getline(file, tp)) {  // c type
        printf("%d\n", tp);
      }
    }
  }
  fclose(file);
  return 0;
}
ocrdu
  • 2,046
  • 6
  • 11
  • 19
  • 1
    Why are you using `printf()` instead of just `std::cout`? Also, `file.is_open()` shouldn't work when `file` is of type `FILE*`... Why aren't you using `std::ifstream`? There's a lot wrong here. – scohe001 Jan 11 '21 at 22:09
  • Does this answer your question? [How to read line by line or a whole text file at once?](https://stackoverflow.com/questions/13035674/how-to-read-line-by-line-or-a-whole-text-file-at-once) – scohe001 Jan 11 '21 at 22:13
  • You may have mis-interpreted the instruct. except for `getline` the rest of the code is C, not C++. `file.is_open()` would sort-of be C++, but `FILE*` has no `is_open` method to call. – user4581301 Jan 11 '21 at 22:26
  • 1
    Ok; are you *supposed* to be doing this in C, or are you supposed to be doing it in C++? I know you've tagged it C++ but this is quite the mix of C and C++, so I just want to confirm. – Jason C Jan 11 '21 at 22:40

1 Answers1

1

Problem:

  1. printf is incorrectly formatted. %d is for signed integers. %s is for strings of characters (More info here). Additionally, if you want to work with printf() you will need a C string or to call the std::string::c_str() function.
  2. You're doing things in C style (using FILE*, fopen(), etc).

Solution:

  1. If you still want or need to use a C style, replace with printf("%d\n", tp); with printf("%s\n", tp.c_str());.
  2. Use a C++ style instead:
  • FILE* -> std::ifstream.
  • fopen() -> std::ifstream::is_open().
  • file < 0 -> std::ifstream::fail().
  • perr -> std::cerr.
  • printf() -> std::cout.
  • fclose() -> std::ifstream::close().

Additional information:

  1. using namespace std; is considered a bad practice (More info here).

Full code:

#include <iostream>
#include <fstream>

int main(int argc, char*argv[])
{
    for(int i = 1; i < argc; i++)
    {
        std::ifstream file(argv[i]);
        if(file.fail())
        {
            std::cerr << "Error opening file.\n";
            return 1;
        }
        std::string tp;
        while(std::getline(file,tp))
            std::cout << tp;
    }
    return 0;
}
GaryNLOL
  • 775
  • 4
  • 16
  • Thanks, I've updated it. – GaryNLOL Jan 11 '21 at 22:29
  • I am downvoting. Even though this is correct, it is not gonna help OP. – Zereges Jan 11 '21 at 22:42
  • Why it is not gonna help OP? – GaryNLOL Jan 11 '21 at 22:47
  • Thank you so much for commenting right here! I just realize that I mix the c++ & c type together, bc professor said we should use fopen() fclose() write() those calls and I have no idea wt those call in the c++. – haonan wang Jan 11 '21 at 23:38
  • `fopen() -> std::ifstream::isOpen()` is wrong if I'm not mistaken. Should be `std::ifstream::open()` instead. –  Jan 12 '21 at 00:03
  • Also, `file.is_open()` is C++. From comments on the OP, that doesn't seem to be in C. The OP would need a C equivalent for that as well. –  Jan 12 '21 at 00:05
  • A more general comment is that while I agree with most of this, you might just want to double-check your C/C++ style differences. The OP has definitely mixed them together very well, and while you generally have separated the C++ from the C, there are a few you have missed. You might want to just do a double check and make sure you haven't missed anything. –  Jan 12 '21 at 00:09
  • Hi, I want to ask, is that fgets, strstr, fputs,fclose,fopen all belongs to C language? Where I can find the equivalent for those? – haonan wang Jan 12 '21 at 06:49
  • Yes, they belong to C.. Their equivalents are `std::getline()`, `std::string::find()`, `std::ofstream::operator< – GaryNLOL Jan 12 '21 at 15:04
  • I think so, I have no idea why my college mainly recommends C++ but our prof asked us mainly use C..... – haonan wang Jan 13 '21 at 02:20
  • how about about fwrite & fread? what's the equivalent for that one. – haonan wang Jan 13 '21 at 02:21
  • Their equivalents are `std::ofstream::write` and `std::ifstream::read`, but I recommend you to use `std::ofstream::operator< – GaryNLOL Jan 13 '21 at 02:45