c-如何将文本文件从DOS格式转换为UNIX格式



我正在尝试用C编写一个程序,该程序读取一个文本文件,并用nrn替换为同一文件,将行结尾从DOS转换为UNIX。我使用fgetc并将该文件视为二进制文件。提前谢谢。

#include <stdio.h>
int main()
{
FILE *fptr = fopen("textfile.txt", "rb+");
if (fptr == NULL)
{
printf("erro ficheiro n");
return 0;
}
while((ch = fgetc(fptr)) != EOF) {
if(ch == 'r') {
fprintf(fptr,"%c", 'n');
} else {
fprintf(fptr,"%c", ch);
}
}
fclose(fptr);
}

如果我们假设文件使用单字节字符集,那么在将文本文件从DOS转换为UNIX时,我们只需要忽略所有'\r'字符。

我们还假设文件的大小小于最高的无符号整数。

我们之所以做这些假设,是为了简化示例。

请注意,下面的示例会按照您的要求覆盖原始文件。通常不应该这样做,因为如果发生错误,可能会丢失原始文件的内容。

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
// Return a negative number on failure and 0 on success.
int main()
{
const char* filename = "textfile.txt";
// Get the file size. We assume the filesize is not bigger than UINT_MAX.
struct stat info;
if (stat(filename, &info) != 0)
return -1;
size_t filesize = (size_t)info.st_size;
// Allocate memory for reading the file
char* content = (char*)malloc(filesize);
if (content == NULL)
return -2;
// Open the file for reading
FILE* fptr = fopen(filename, "rb");
if (fptr == NULL)
return -3;
// Read the file and close it - we assume the filesize is not bigger than UINT_MAX.
size_t count = fread(content, filesize, 1, fptr);
fclose(fptr);
if (count != 1)
return -4;
// Remove all 'r' characters 
size_t newsize = 0;
for (long i = 0; i < filesize; ++i) {
char ch = content[i];
if (ch != 'r') {
content[newsize] = ch;
++newsize;
}
}
// Test if we found any
if (newsize != filesize) {
// Open the file for writing and truncate it.
FILE* fptr = fopen(filename, "wb");
if (fptr == NULL)
return -5;
// Write the new output to the file. Note that if an error occurs,
// then we will lose the original contents of the file.
if (newsize > 0)
count = fwrite(content, newsize, 1, fptr);
fclose(fptr);
if (newsize > 0 && count != 1)
return -6;
}
// For a console application, we don't need to free the memory allocated
// with malloc(), but normally we should free it.
// Success 
return 0;
} // main()

只删除'\r'后面跟'\n',用这个循环替换循环:

// Remove all 'r' characters followed by a 'n' character
size_t newsize = 0;
for (long i = 0; i < filesize; ++i) {
char ch = content[i];
char ch2 = (i < filesize - 1) ? content[i + 1] : 0;
if (ch == 'r' && ch2 == 'n') {
ch = 'n';
++i;
}
content[newsize++] = ch;
}

相关内容

  • 没有找到相关文章

最新更新