如何在C语言中将文件的特定内容复制到另一个文件上?
我已经做了几个小时了,我觉得我没有掌握C语言中的一些基本技术。我一直在尝试读取特定的行,并将其从一个文件存储到两个单独的文件中。源文件(temp)是我正在读取的文件,数据和文本是我试图存储的两个文件 TL;DR,如何将一个文件中的内容拆分为两个单独的文件 例如,输入文件是:如何在C语言中将文件的特定内容复制到另一个文件上?,c,file-io,C,File Io,我已经做了几个小时了,我觉得我没有掌握C语言中的一些基本技术。我一直在尝试读取特定的行,并将其从一个文件存储到两个单独的文件中。源文件(temp)是我正在读取的文件,数据和文本是我试图存储的两个文件 TL;DR,如何将一个文件中的内容拆分为两个单独的文件 例如,输入文件是: .data A: .word 32 B: .word 16 .text main: la $s0 A lw $s0 0($s0) la $s1 B lw $s1 0($s1) 此函数将接收临时文件,并将.data和.
.data
A: .word 32
B: .word 16
.text
main:
la $s0 A
lw $s0 0($s0)
la $s1 B
lw $s1 0($s1)
此函数将接收临时文件,并将.data和.text段处理为单独的文件
void process_file(FILE * temp)
{
FILE * data, * text;
char * token;
char * nextLine;
char line[256];
char * str_token = malloc(256 * sizeof(char));
char * str = malloc(256 * sizeof(char));
int i;
data = fopen("data.asm", "w");
text = fopen("text.asm", "w");
if (temp == NULL || data == NULL || text == NULL)
{
printf("UNABLE TO OPEN OR LOCATE FILES. EXITING.\n");
exit(1);
}
while (!feof(temp))
{
if ((nextLine = fgets(line, 256, temp)) != NULL)
{
token = strtok(nextLine, "\n");
//printf("%s", nextLine);
if (token != NULL)
{
strcat(str_token, token);
token = strtok(NULL, "\n");
if ((strstr(str_token, ".data")) != NULL)
{
strcat(str, str_token);
fprintf(data, "%s", str); //ADDING THIS CAUSES A SEG FAULT :(
}
else if ((strstr(token, ".text")) != NULL)
{
fprintf(text, "%s", token);
token = strtok(NULL, "\n");
}
}
}
}
}
谢谢 这就是我用你提供的代码得到你答案的方法
void process_file(FILE *temp){
FILE *data, *text, *fp;
char line[256], str[256];
char *token;
data = fopen("data.asm", "w");
text = fopen("text.asm", "w");
if (temp == NULL || data == NULL || text == NULL) {
printf("UNABLE TO OPEN OR LOCATE FILES. EXITING.\n");
exit(1);
}
fp = data;//default for not start with .data and .text
while(fgets(line, sizeof line, temp)){
strcpy(str, line);
if((token=strtok(str, " \t\n"))!=NULL){//not blank line
if(strcmp(token, ".data")==0)
fp = data;
else if(strcmp(token, ".text")==0)
fp = text;
}
fprintf(fp, "%s", line);
}
fclose(data);
fclose(text);
}
void process_file(FILE * temp)
{
//You may use NULL versus 0
FILE * data = 0, * text = 0;
char * token = 0;
char * nextLine = 0;
char line[256];
int i = 0;
data = fopen("data.asm", "w");
text = fopen("text.asm", "w");
if ( 0 == temp || 0 == data || 0 == text )
{
printf("UNABLE TO OPEN OR LOCATE FILES. EXITING.\n");
exit(1);
}
while (!feof(temp))
{
if ((nextLine = fgets(line, 256, temp)) != NULL)
{
token = strtok(nextLine, "\n");
//printf("%s", nextLine);
if (0 != token){
if(strcmp(token, ".data") == 0 ){
//Make use of that i
i = 1;
}
else if(strcmp(token, ".text") == 0){
i = 2;
}
switch(i){
case 1:
fprintf(data, "%s\r\n", token);
break;
case 2:
fprintf(text, "%s\r\n", token);
break;
default:
printf("Invalid!");
break;
}
}
}
}
fclose(data);
fclose(text);
fclose(temp);
}#包括
#包括
作废进程文件(文件*临时文件)
{
文件*data,*text,*write=NULL;
字符*令牌;
char*nextLine;
字符行[256];
char*str_token=malloc(256*sizeof(char));
char*str=malloc(256*sizeof(char));
char val[]=“safasfsdf”;
int i;
data=fopen(“data.asm”,“w”);
text=fopen(“text.asm”,“w”);
if(temp==NULL | | data==NULL | | text==NULL)
{
printf(“无法打开或定位文件。正在退出。\n”);
出口(1);
}
而(!feof(temp))
{
如果((nextLine=fgets(第256行,温度))!=NULL)
{
if((strstrstr(行,.data))!=NULL)
{
写入=数据;
}
else if((strstrstr(行,.text))!=NULL)
{
写=文本;
}
如果(第[0]行)!='\n'){
printf(“%p%s”,写入,行);
fprintf(写入,“%s”,行);
}
}
}
fclose(数据);
fclose(文本);
}
首先确定模式。根据模式选择文件指针。我已经从原始源代码中删除了一些不需要的行,并使其变得简单。希望这将对您有所帮助。看起来您一直在附加到
str
和str\u令牌
,但从未清除它们。我猜最终你会写超过分配内存的末尾。是否只是尝试从.data中写入每一行,直到将.text行写入一个文件,将其余的写入另一个文件?现在还不清楚所有的strok都是干什么用的。请使用strlcat()
而不是strcat()
。它更安全,并将帮助您避免将来出现类似问题。您是否被错误或路由输出所困扰?在代码中添加更多注释,描述它应该做什么,会有所帮助。您的while(!feof(temp))
循环浪费了一定程度的缩进;您应该删除循环,并在的同时使用更改的if((nextline=fgets(line,256,temp))!=NULL)
。你也可以放弃nextline
作业;该值将等于行
(当它不为空时,但已被条件过滤掉)。这修复了它!非常感谢!:)
#include <string.h>
#include <stdio.h>
void process_file(FILE * temp)
{
FILE * data, * text, *write = NULL;
char * token;
char * nextLine;
char line[256];
char * str_token = malloc(256 * sizeof(char));
char * str = malloc(256 * sizeof(char));
char val [] = "safasfsdf";
int i;
data = fopen("data.asm", "w");
text = fopen("text.asm", "w");
if (temp == NULL || data == NULL || text == NULL)
{
printf("UNABLE TO OPEN OR LOCATE FILES. EXITING.\n");
exit(1);
}
while (!feof(temp))
{
if ((nextLine = fgets(line, 256, temp)) != NULL)
{
if ((strstr(line, ".data")) != NULL)
{
write = data;
}
else if ((strstr(line, ".text")) != NULL)
{
write = text;
}
if(line[0]!= '\n'){
printf ("%p %s",write, line);
fprintf(write, "%s", line);
}
}
}
fclose (data);
fclose (text);
}