Skip to content

Instantly share code, notes, and snippets.

@gnitnaw
Last active August 29, 2015 14:22
Show Gist options
  • Save gnitnaw/11ad7e7a98e4ebc8601f to your computer and use it in GitHub Desktop.
Save gnitnaw/11ad7e7a98e4ebc8601f to your computer and use it in GitHub Desktop.
char separate
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 256
#define NITEM 15
int getData(char* line, char** t);
void outputResult(FILE *fout, char** title, char** t, int N);
int main(void) {
char s;
int i, j, N;
char **t = (char**)malloc(sizeof(char*)*NITEM);
char **title = (char**)malloc(sizeof(char*)*NITEM);
char* line = (char*)malloc(sizeof(char)*SIZE);
FILE *fp = fopen("Example_table.txt", "r");
FILE *fout = fopen("output.txt", "a");
if (!fp) {
perror("Error! Cannot find the file");
exit(1);
}
if (!fout) {
perror("Error! Cannot create the file");
exit(2);
}
fgets(line,SIZE,fp);
N = getData(line,t);
for (i=0; i<N; ++i) {
title[i] = malloc(strlen(t[i])+1);
strcpy(title[i], t[i]);
}
while(!feof(fp)) {
fgets(line,SIZE,fp);
j = getData(line, t);
if (j<=1) continue;
outputResult(stdout,title,t,j);
outputResult(fout,title,t,j);
}
for (i=0; i<N; ++i) {
free(title[i]);
}
free(line);
fclose(fp);
fclose(fout);
free(t);
free(title);
return 0;
}
int getData(char* line, char** t) {
int item=0;
char *c = strtok(line,"\n");
c = strtok(line,"\t");
t[item++] = c;
while (c != NULL && item < NITEM) {
c = strtok(NULL,"\t");
if (c!= NULL) t[item++] = c;
}
return item;
}
void outputResult(FILE *fout, char** title, char** t, int N) {
int i, a;
for (i=0; i<N;++i) {
fprintf(fout, "%s : %s ", title[i], t[i]);
if (i!=N-1) fprintf(fout, ", ");
}
fputc('\n',fout);
}
@gnitnaw
Copy link
Author

gnitnaw commented Jun 12, 2015

簡而言之,我希望能把每行資料讀進char _line,然後透過getData函式
把line裏面的item(用\t分開)變成char_們 都輸入char** t 裏面,
然後getData回傳item個數
這樣我就可以用t[i]去做後續的處理...不知道這樣想法對不對

@gnitnaw
Copy link
Author

gnitnaw commented Jun 12, 2015

自己好好想了一下總算解決了...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment