我面临C++和C的问题,其中我的ifstream对象或文件指针不能正确读取文本文件,并在输出时显示非法字符。但是,当我读取.dat文件时,它会输出正确的结果。Turbo C++编译器没有正确读取文本文件。 (只有当我把它做成一个.dat文件时,它是否正确读取)
这是C代码:
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
void main() {
FILE *file;
char ch;
file = fopen("code.dat", "r");
while((ch = getc(file)) != EOF)
printf("%c", ch);
getch();
fclose(file);
}
这是CPP代码:
#include <fstream.h>
#include <iostream.h>
#include <conio.h>
#include <string.h>
int main() {
clrscr();
fstream file;
file.open("code.dat", ios::in);
char ch, c;
char token[6];
int id = 0, op = 0, key = 0;
while (!file.eof()) {
file >> ch;
if(ch == ' ') {
if ((ch > 64 && ch < 91) || (ch > 96 && ch < 123))
id += 1;
}
}
cout << id;
file.close();
getch();
return 0;
}
Turbo C++是一个古老的编译器,已停止支持。它在语言标准化之前使用C++方言。如果你想学习C++,它的用法就是升级到现代。 GCC和Clang是不错的选择。 – StoryTeller
“我正面临C++和C的问题,我的ifstream对象在哪里” - 您不知道。 C没有“ifstream对象”。这是一种完全不同的语言! – Olaf
[为什么不用Turbo C++编译简单的“Hello World”风格的程序?](https://stackoverflow.com/questions/44863062/why-doesnt-a-simple-hello-world-style -program-compile-with-turbo-c) –