Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/189.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在c/c+中显示txt文件中的文字+;_C++_C - Fatal编程技术网

C++ 在c/c+中显示txt文件中的文字+;

C++ 在c/c+中显示txt文件中的文字+;,c++,c,C++,C,我有个小问题。我有一个只包含英文单词的文本文件。我只想显示文件中的单词,忽略空格。 代码如下: #include<iostream.h> #include<conio.h> #include<fstream.h> #include<stdio.h> #define max 50 void main() { clrscr(); char output; FILE *p; char a[max]; int i=0

我有个小问题。我有一个只包含英文单词的文本文件。我只想显示文件中的单词,忽略空格。 代码如下:

#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#define max 50
void main()
{
    clrscr();
    char output;
    FILE *p;
    char a[max];
    int i=0;
    p=fopen("thisfile.txt","r");
        while(1)
        {
            char ch=fgetc(p);
            if(ch==EOF)
            {
                break;
            }
            else if(ch==' ')
            {
                cout<<a;
                delete [] a;
                            i=0;
            }
            else
            {
                a[i++]=ch;
            }
        }
    fclose(p);
    getch();
}
#包括
#包括
#包括
#包括
#定义最大值50
void main()
{
clrsc();
字符输出;
文件*p;
字符a[max];
int i=0;
p=fopen(“thisfile.txt”,“r”);
而(1)
{
char ch=fgetc(p);
如果(ch==EOF)
{
打破
}
else if(ch='')
{
cout这里有一个非常简单的解决方案:

#include <string>
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream infile("thisfile.txt");

    for (std::string word; infile >> word; )
    {
        std::cout << "Got one word: " << word << std::endl;
    }
}
#包括
#包括
#包括
int main()
{
std::ifstream infle(“thisfile.txt”);
对于(std::string word;infle>>word;)
{
std::cout这里有一个非常简单的解决方案:

#include <string>
#include <fstream>
#include <iostream>

int main()
{
    std::ifstream infile("thisfile.txt");

    for (std::string word; infile >> word; )
    {
        std::cout << "Got one word: " << word << std::endl;
    }
}
#包括
#包括
#包括
int main()
{
std::ifstream infle(“thisfile.txt”);
对于(std::string word;infle>>word;)
{

std::cout一些变量没有初始化。(顺便说一句,
fgetc
返回一个
int
,根据
EOF
进行测试很重要)标准C中一个更简单的解决方案:

#include <stdio.h>

int c;

while ((c = getchar()) != EOF) {
     if (c == ' ')
         putchar('\n');
     else
         putchar(c);
}
#包括
INTC;
而((c=getchar())!=EOF){
如果(c='')
putchar('\n');
其他的
普查尔(c);
}

您的一些变量没有初始化。(顺便说一句,
fgetc
返回一个
int
,重要的是要测试
EOF
)标准C中更简单的解决方案:

#include <stdio.h>

int c;

while ((c = getchar()) != EOF) {
     if (c == ' ')
         putchar('\n');
     else
         putchar(c);
}
#包括
INTC;
而((c=getchar())!=EOF){
如果(c='')
putchar('\n');
其他的
普查尔(c);
}

这里有一种方法可以让你反复浏览这些单词:

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

int main()
{
  std::ifstream file("test.txt");
  std::istream_iterator<std::string> begin(file), end;
  for(; begin!= end; ++ begin)
    std::cout<< *begin<< '\n';
}
#包括
#包括
#包括
#包括
#包括
int main()
{
std::ifstream文件(“test.txt”);
istream_迭代器开始(文件),结束;
for(;begin!=end;++begin)

std::cout这里有一种方法可以让你反复阅读单词:

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

int main()
{
  std::ifstream file("test.txt");
  std::istream_iterator<std::string> begin(file), end;
  for(; begin!= end; ++ begin)
    std::cout<< *begin<< '\n';
}
#包括
#包括
#包括
#包括
#包括
int main()
{
std::ifstream文件(“test.txt”);
istream_迭代器开始(文件),结束;
for(;begin!=end;++begin)

你为什么不试试这个:

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main() {
   clrscr();
   string s;
   ifstream in("file.txt");

   while(in >> s) {
     cout << s << endl;
   }

   in.close();

   return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
clrsc();
字符串s;
ifstream in(“file.txt”);
while(在>>s中){

你为什么不试试这个:

#include <iostream>
#include <fstream>
#include <string>
#include <conio.h>

using namespace std;

int main() {
   clrscr();
   string s;
   ifstream in("file.txt");

   while(in >> s) {
     cout << s << endl;
   }

   in.close();

   return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
int main(){
clrsc();
字符串s;
ifstream in(“file.txt”);
while(在>>s中){

我喜欢简明扼要:

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

int main() {
   std::copy(std::istream_iterator<std::string>(std::ifstream("test.txt") >> std::ws),
             std::istream_iterator<std::string>(),
             std::ostream_iterator<std::string>(std::cout));
}
#包括
#包括
#包括
#包括
int main(){
std::copy(std::istream_迭代器(std::ifstream(“test.txt”)>>std::ws),
std::istream_迭代器(),
std::ostream_迭代器(std::cout));
}

当然,您可能需要在单词之间打印一些东西…

我喜欢简洁明了:

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

int main() {
   std::copy(std::istream_iterator<std::string>(std::ifstream("test.txt") >> std::ws),
             std::istream_iterator<std::string>(),
             std::ostream_iterator<std::string>(std::cout));
}
#包括
#包括
#包括
#包括
int main(){
std::copy(std::istream_迭代器(std::ifstream(“test.txt”)>>std::ws),
std::istream_迭代器(),
std::ostream_迭代器(std::cout));
}

当然,你可能想在单词之间打印一些东西…

首先,使用正确的英语,然后,给我们输入数据和预期的输出。删除[]a;你为什么要这样做?这会产生不好的结果。非常糟糕。询问者似乎认为
删除[]a
将清除字符数组,以便它可以用于下一个单词。提示:不会。每个
删除
删除[]
都应该有一个匹配的
新建
新建[]
,反之亦然。您从未为数组分配过
新建[]
,因此您肯定不需要
删除[]。我注意到这个文件使用了非常过时的标头!所有的标准C++库头都不使用<代码> .H/COD>后缀。你应该立即摆脱你使用的任何C++教程,用一个只有十年历史的,或者更好的,但最新的一个……首先使用适当的英语,和SECO。nd,给我们输入数据和预期输出。删除[]a;你为什么要这样做?这会产生不好的结果。真的很糟糕。询问者似乎认为
delete[]a
会清除字符数组,以便它可以用于下一个单词。提示:不会。每个
delete
delete[]
应该有一个匹配的
new
new[]
,反之亦然。您从未使用
new[]
分配过数组,因此您肯定不需要
删除[]。我注意到这个文件使用了非常过时的标题!所有的标准C++库头都不使用<代码> .H/COD>后缀。你应该立即摆脱你正在使用的C++教程,用一个只有十年的旧教程来代替它。所有那些
std::
前缀?为了简洁起见,只需将
使用名称空间std
放在main()中同样的,对于其他C++例子,我知道教条不是使用<代码>使用命名空间STD< /C> >,但是如果保持在函数内,似乎是两个邪恶的较小。@ WilliamMorris:我发现对所有的名称都有资格(如果它们是合格的)对我来说非常有效。在某些情况下,你需要这样做。(例如,处理模板中的从属名称)。主要原因是它可以更容易地跟踪名称的来源。当然,我现在非常了解名称空间
std
中的名称,但在实际代码中,我通常在一个典型的翻译单元中使用几十个左右的附加名称空间。我认为,指导人们在哪里查找定义是有帮助的。我倾向于使用短名称空间别名。点:-我正在学习C++(从C背景),我发现这些长字符串包含重复<代码> STD::/Cord>把我推到墙上!它是ReA吗?