如何将文件的特定内容复制到C中的另一个文件中?

时间:2014-10-28 23:58:31

标签: c file-io

我已经在这几个小时了,我觉得我没有在C中获得一些基本技术。我一直在尝试读取特定行并将其存储在一个文件中的2个单独文件中。源文件(temp)是我正在读取的文件,2个文件,数据和文本是我要存储的2个文件。

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和.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");
                }
            }
        }
    }
}

谢谢!

3 个答案:

答案 0 :(得分:1)

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);
}

答案 1 :(得分:0)

这就是我以前用你提供的代码得到你的答案

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);

}

答案 2 :(得分:0)

#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);

}
首先识别

模式。基于模式文件指针被选中。我已从原始源中删除了一些不需要的行并使其变得简单。希望这对你有所帮助。