C++ 无法将整个文本文件复制到字符数组

C++ 无法将整个文本文件复制到字符数组,c++,visual-c++,C++,Visual C++,我试图使用fstream将整个文本文件复制到char数组中,但即使增加数组的大小,它也会将文本文件读取到相同的限制。我很乐意将其保存在char数组中,如果它不是动态数组,那就好了???任何解决方案请 // smallGrams.cpp : Defines the entry point for the console application. // //#include "stdafx.h" #include<iostream> using namespace std; #inc

我试图使用fstream将整个文本文件复制到char数组中,但即使增加数组的大小,它也会将文本文件读取到相同的限制。我很乐意将其保存在char数组中,如果它不是动态数组,那就好了???任何解决方案请

 // smallGrams.cpp : Defines the entry point for the console application.
//

//#include "stdafx.h"
#include<iostream>
using namespace std;
#include<string>
#include<fstream>

void readInput(const char* Path);
 void removePunctucationMarks();
 void removeSpacing();
 void insertDots();
 char * getText();
 void generateUnigrams();
  void generateBigrams();
  void generateTrigrams();
 double validateSentance(string str);
 string sentenceCreation(int position);

 int main()
 {
      char *path="alice.txt";
      readInput(path);

     return 0;
 }
 void readInput(const char* Path)
 {
     ifstream infile;


     infile.open(Path);

     if(!infile.fail())
         cout<<"File opened successfully"<<endl;
     else
         cout<<"File failed to open"<<endl;

     int arrSize=100000000;
     char *arr=new char[arrSize];
     int i=0;
     while(!infile.eof()&&i<arrSize)
     {
         infile.get(arr[i]);
         i++;
     }
     arr[i-1]='\0';
     for(short i=0;i<arrSize&&arr[i]!='\0';i++)
     {
         cout<<arr[i];
     }





 }
//smallGrams.cpp:定义控制台应用程序的入口点。
//
//#包括“stdafx.h”
#包括
使用名称空间std;
#包括
#包括
无效读取输入(常量字符*路径);
void RemovePunctionMarks();
void removeSpacing();
void insertDots();
char*getText();
无效生成语法();
无效生成图();
无效生成图();
双重验证状态(字符串str);
字符串语句创建(int位置);
int main()
{
char*path=“alice.txt”;
读取输入(路径);
返回0;
}
无效读取输入(常量字符*路径)
{
河流充填;
填充。开放(路径);
如果(!infle.fail())

cout当您使用for循环读取和显示数组内容时,而不是从文件中读取数据时,可能会出现错误。
使用int而不是short-in-for-loop,因为short只能增加到32768。

这里使用一种简单的倍增方法

#include<iostream>
#include<string>
#include<fstream>
#include <cstdint>
#include <cstring>

using namespace std;

void readInput(const char* Path)
{
     ifstream infile;


     infile.open(Path);

     if(!infile.fail())
         cout<<"File opened successfully"<<endl;
     else{
         cout<<"File failed to open"<<endl;
         return;
     }

     int capacity=1000;
     char *arr=new char[capacity];
     char *temp;

     int i=0;

     while(infile >> arr[i])
     {
         i++;
         if ( i >= capacity ) {

             temp = new char[capacity*2];
             std::memcpy(temp , arr, capacity);
             delete [] arr;
             arr = temp;
             capacity *=2;
         }
     }
 }

int main()
{
    char *path="alice.txt";
    readInput(path);

    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
无效读取输入(常量字符*路径)
{
河流充填;
填充。开放(路径);
如果(!infle.fail())

cout这是一个C风格的解决方案,可以工作。它检查文件大小,然后为数组分配必要的内存,并在一次调用中读取文件的所有内容。fread()调用返回您请求的字节数或出现错误(检查引用)

#包括
#包括
#包括
int main(int argc,char*argv[]){
字符*数据;
国际数据中心;
文件*fd;
fd=fopen(“file.txt”,“r”);
如果(fd==NULL){
//错误
返回-1;
}
fseek(fd,0,SEEK_END);
数据长度=ftell(fd);
倒带(fd);
数据=(字符*)malloc((数据长度+1)*大小(字符));
memset(数据,数据长度+1,空);
if(fread(数据,大小(字符),数据长度,fd)!=数据长度){
//错误
返回-1;
}
printf(“%s\n”,数据);
fclose(fd);
免费(数据);
返回0;
}

std::string有什么问题?@Yksisarvinen我们必须在赋值检查中使用char数组,方法是在最后一个for循环中将short改为unsing int。文件的内容是什么?输出是什么?您是否调试了代码以查看循环何时结束以及在什么条件下结束?您知道循环何时结束和结束吗如何工作?我们必须在作业中使用字符数组。您可以向您的老师发送此链接:(可能是匿名的。)-)看是的。你完全正确。我刚刚修改了一小部分,现在已经完成,并且正在运行。当文件无法打开时,仍然存在OP的继续错误。如果文件大于
int
的容量会发生什么?我只回答了Mujeeb Alam的问题。你可能想使用
fseek(fd,0,SEEK\u BEGIN);
来倒带文件。我不确定
rewind()
函数是否会关闭文件或要求关闭文件。此外,您不需要
sizeof(char)
,因为
char
的大小定义为1。
# include <cstring>
# include <cstdlib>
# include <cstdio>

int main(int argc, char *argv[]) {
    char *data;
    int data_len;
    FILE *fd;

    fd = fopen ("file.txt", "r");
    if (fd == NULL) {
        // error
        return -1;
    }

    fseek (fd , 0 , SEEK_END);
    data_len = ftell (fd);
    rewind (fd);

    data = (char *) malloc ((data_len + 1) * sizeof (char));
    memset (data, data_len + 1, NULL);

    if (fread (data, sizeof (char), data_len, fd) != data_len) {
        // error
        return -1;
    }

    printf ("%s\n", data);

    fclose (fd);
    free (data);

    return 0;
}