Reputation: 457
I've been at this for hours now and I feel like I'm not getting some fundamental techniques in C. I've been trying to read in a specific line and store it into 2 separate files from one file. The source file (temp) is the one I'm reading in and the 2 files, data and text are the 2 files I'm trying to store.
TL;DR, how do I split up contents from a file into two separate file?
For example, the input file is:
.data
A: .word 32
B: .word 16
.text
main:
la $s0 A
lw $s0 0($s0)
la $s1 B
lw $s1 0($s1)
This function will take in the temp file and process the .data and .text segments into separate files.
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");
}
}
}
}
}
Thanks!
Upvotes: 0
Views: 1666
Reputation: 1323
#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);
}
pattern is identified first. Based on the pattern file pointer is selected. I have removed the few unwanted line from the original source and make it simple. Hope this will be helpful to you.
Upvotes: 0
Reputation: 503
This is what I used to get your answer, using the code you provided anyway
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);
}
Upvotes: 0
Reputation: 40155
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);
}
Upvotes: 1