Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.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
如何使用stdio.hc库读取文本文件并将内容写入另一个文本文件_C_File Io - Fatal编程技术网

如何使用stdio.hc库读取文本文件并将内容写入另一个文本文件

如何使用stdio.hc库读取文本文件并将内容写入另一个文本文件,c,file-io,C,File Io,我编写了如下函数,用于读取文本文件并将内容写入另一个具有不同文件名的文本文件: 读取文件功能: char *getFileContent (const char *fileName) { char errorBuffer[50]; //Prepare read file FILE *pReadFile; long bufferReadSize; char *bufferReadFile; //This variable is going to be returned as file conte

我编写了如下函数,用于读取文本文件并将内容写入另一个具有不同文件名的文本文件:

读取文件功能:

char *getFileContent (const char *fileName)
{
char errorBuffer[50];

//Prepare read file
FILE *pReadFile;
long bufferReadSize;
char *bufferReadFile; //This variable is going to be returned as file content
size_t readFileSize;

pReadFile = fopen (fileName, "rb");

if (pReadFile != NULL)
{
    // Get file size.
    fseek (pReadFile , 0 , SEEK_END);
    bufferReadSize = ftell (pReadFile);
    rewind (pReadFile);

    // Allocate RAM to contain the whole file:
    bufferReadFile = (char*) malloc (sizeof(char) * bufferReadSize);

    if (bufferReadFile != NULL) 
    {
        // Copy the file into the buffer:
        readFileSize = fread (bufferReadFile, sizeof(char), bufferReadSize, pReadFile);

        if (readFileSize == bufferReadSize) 
        {
            return bufferReadFile;

            fclose (pReadFile);
            free (bufferReadFile);
        } else {
            //fread failed              
            sprintf (errorBuffer, "File reading failed for file:\n%s", fileName);
            MessageBox (NULL, errorBuffer, "Error file reading", MB_ICONERROR | MB_OK);
        }
    } else {
        //malloc failed
        sprintf (errorBuffer, "Memory allocation failed for file:\n%s", fileName);
        MessageBox (NULL, errorBuffer, "Error memory allocation", MB_ICONERROR | MB_OK);
    }       
} else {
    //fopen failed
    sprintf (errorBuffer, "File opening failed for file:\n%s", fileName);
    MessageBox (NULL, errorBuffer, "Error file opening", MB_ICONERROR | MB_OK);
}
}
//Get file content from read file
char *fileContent = getFileContent (readFileName);
FILE *pWriteFile = fopen (writeFileName, "wb");
fwrite (fileContent, sizeof (char), strlen (fileContent), pWriteFile);
fclose (pWriteFile);
写入文件代码:

char *getFileContent (const char *fileName)
{
char errorBuffer[50];

//Prepare read file
FILE *pReadFile;
long bufferReadSize;
char *bufferReadFile; //This variable is going to be returned as file content
size_t readFileSize;

pReadFile = fopen (fileName, "rb");

if (pReadFile != NULL)
{
    // Get file size.
    fseek (pReadFile , 0 , SEEK_END);
    bufferReadSize = ftell (pReadFile);
    rewind (pReadFile);

    // Allocate RAM to contain the whole file:
    bufferReadFile = (char*) malloc (sizeof(char) * bufferReadSize);

    if (bufferReadFile != NULL) 
    {
        // Copy the file into the buffer:
        readFileSize = fread (bufferReadFile, sizeof(char), bufferReadSize, pReadFile);

        if (readFileSize == bufferReadSize) 
        {
            return bufferReadFile;

            fclose (pReadFile);
            free (bufferReadFile);
        } else {
            //fread failed              
            sprintf (errorBuffer, "File reading failed for file:\n%s", fileName);
            MessageBox (NULL, errorBuffer, "Error file reading", MB_ICONERROR | MB_OK);
        }
    } else {
        //malloc failed
        sprintf (errorBuffer, "Memory allocation failed for file:\n%s", fileName);
        MessageBox (NULL, errorBuffer, "Error memory allocation", MB_ICONERROR | MB_OK);
    }       
} else {
    //fopen failed
    sprintf (errorBuffer, "File opening failed for file:\n%s", fileName);
    MessageBox (NULL, errorBuffer, "Error file opening", MB_ICONERROR | MB_OK);
}
}
//Get file content from read file
char *fileContent = getFileContent (readFileName);
FILE *pWriteFile = fopen (writeFileName, "wb");
fwrite (fileContent, sizeof (char), strlen (fileContent), pWriteFile);
fclose (pWriteFile);
他们成功地一起读写文件。然而,在写入的文件中,在其结尾,一些奇怪的字符出现如下:

ý

请帮我解决这个问题。当原始文件中不存在最后的奇怪字符时,如何避免写入文件中的这些字符

fwrite (fileContent, sizeof (char), strlen (fileContent), pWriteFile);
strlen
()在这里不起作用,因为fileContent包含二进制数据。二进制数据可能包含一个空字节,这意味着
strlen
()太短,或者它可能不包含一个空字节,这意味着
strlen
()将读取过去的
fileContent
,直到找到一个空字节为止。这就是为什么你在最后看到垃圾的原因

另外请注意,在读取例程中,
fclose
()和
free
()不会发生,因为它们位于
return
语句之后。但是,请注意,在编写数据之前,您不能
释放数据

另一方面,如果它不是二进制文件,则只需要在数据末尾终止0,然后strlen()就可以工作了。因此,在读取过程中,您需要分配另一个字节,并确保该字节为零:

bufferReadFile = (char*) malloc (sizeof(char) * bufferReadSize + 1); // note the + 1
bufferReadFile[bufferReadSize] = 0; // the terminating null byte.

正如注释一样,在读取例程中,fclose和free永远不会发生,因为它们位于return语句之后。如果这是一个二进制文件,则不能使用strlen()确定数据的长度。您需要从读取例程中获取数据的大小。输入文件的内容是什么?你能给我们看看吗?