存储从一个文件中的字符串到数组 [英] Storing Strings from a File to an Array
问题描述
我从两个文件读取的字符串,并将它们存储为两个独立的数组,然后,我尝试对它们进行排序,并将其写入到另一个文件......这部分工作是应该的方式,但我真正需要做的是从两个数组成一个组合的字符串,这样我可以按字母顺序进行排序,然后写入文件,但问题是,当我尝试这两个数组存储到一个阵列我得到段错误作为错误信息..我真的不知道如何这两个数组存储到另一个,这样我可以为了对它进行排序......我知道如何排序呢,我只是不知道如何阅读这些到另一个阵列...我不能使用#DEFINE因为我将所有这一切都写进去了测试code ...
函数我已经试过类似
新的[I] = str中;
块引用>和也:
的strcpy(新[I],STR)
块引用>但无论这些工作...任何帮助将大大AP preciated ...
下面是我的code:
#包括LT&;&stdio.h中GT;
#包括LT&;&string.h中GT;主要(无效)
{
焦炭海峡[200];
焦炭STR2 [300];
烧焦新[300];
炭温度[300];
INT行号= 0;
FILE * FA =的fopen(book1.dat,R);
FILE * FB =的fopen(book2.dat,R);
FILE * FC =的fopen(fixed.txt,W); INT I = 0;
INT J = 0;
时int k; / *读字符串到一个数组,而不是文件*年底/
而(的feof(FA)及!&安培;!的feof(FB))
{
行号++;
的fscanf(FA,%[^ \\ n] \\ n,STR);
的fscanf(FB,%[^ \\ n] \\ n,STR2); / *数组比较字符串并写入到阵列* /
如果(的strcmp(STR2,STR)℃的)
{
fprintf中(FC,%S \\ n,STR2);
fprintf中(FC,%S \\ n,STR);
} 如果(的strcmp(STR2,STR)大于0)
{
fprintf中(FC,%S \\ n,STR);
fprintf中(FC,%S \\ n,STR2)
} / *打印出海峡*的结果/
的printf(%S,STR);
}
}
解决方案的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;的typedef的char *类型;typedef结构向量{
为size_t的大小;
为size_t能力;
键入*阵列;
}向量;矢量* vec_make(){
矢量* V;
V =(向量*)malloc的(的sizeof(向量));
V->大小= 0;
V->容量= 16;
V->阵=(类型*)的realloc(NULL,sizeof的(向量)*(V->容量* = 2));
返回伏;
}无效vec_add(矢量* V,类型值){
V->阵[V->大小] =值;
如果(++ V->大小== V->能力)
V->阵=(类型*)的realloc(V->阵列的sizeof(向量)*(V->容量* = 2));
}为size_t vec_size(矢量* V){
返回V->大小;
}类型* vec_getArray(矢量* V){
返回V->数组;
}无效vec_free(矢量* V){
免费(V->数组);
免费(V);
}INT CMP(常量无效*一,常量无效* B){
返回STRCMP(*(字符**)A,*(字符**)B);
}无效合并(为const char * inpfile1,为const char * inpfile2,为const char * OUTFILE){
FILE * FP;
字符的buff [256],**阵列;
矢量* V;
为size_t C,尺寸; V = vec_make();
FP = FOPEN(inpfile1,R);
而(NULL!=与fgets(BUFF,256,FP))
vec_add(V,的strdup(BUFF));
FCLOSE(FP); FP = FOPEN(inpfile2,R);
而(NULL!=与fgets(BUFF,256,FP))
vec_add(V,的strdup(BUFF));
FCLOSE(FP); 阵= vec_getArray(ⅴ);
大小= vec_size(ⅴ);
//的qsort(数组,大小的sizeof(类型),CMP);
FP = FOPEN(OUTFILE,W);
为(C = 0;℃下大小++三){
的fputs(数组[C],FP);
免费(数组[C]);
}
FCLOSE(FP);
vec_free(五);
}诠释主(){
合并(book1.txt,book2.txt,fixed.txt);
返回0;
}I have read the strings from two files and stored them into two separate arrays, I then tried sorting them and writing them into another file...This part works the way it is supposed to but what I really need to do is combine the strings from the two arrays into one so that i can sort them in alphabetical order and then write to a file but the problem is that when i try to store these two arrays into one array I get segmentation fault as the error message..I am really not sure how to store these two arrays into another one so that I can sort it in order...I know how to sort it I am just not sure how to read these into an another array... i cant use #DEFINE because I am going to write all of this into a function for a tester code...
I have tried something like
new[i] = str;
and also:
strcpy(new[i],str)
but neither of these work...any help would be greatly appreciated...
Here is my code:
#include<stdio.h> #include<string.h> main (void) { char str[200]; char str2[300]; char new[300]; char temp [300]; int linenumber=0; FILE *fa = fopen ("book1.dat", "r"); FILE *fb = fopen ("book2.dat", "r"); FILE *fc = fopen ("fixed.txt", "w"); int i=0; int j=0; int k; /*read the strings into an array while it is not the end of file*/ while(!feof(fa)&& !feof(fb)) { linenumber++; fscanf(fa,"%[^\n]\n",str); fscanf(fb,"%[^\n]\n",str2); /*compare strings in array and write to array*/ if(strcmp(str2, str)<0) { fprintf(fc, "%s\n", str2); fprintf(fc, "%s\n", str); } if (strcmp(str2,str)>0) { fprintf(fc, "%s\n", str); fprintf(fc, "%s\n", str2) } /*print out the results of str */ printf("%s", str); } }
解决方案#include <stdio.h> #include <stdlib.h> #include <string.h> typedef char* Type; typedef struct vector { size_t size; size_t capacity; Type *array; } Vector; Vector *vec_make(){ Vector *v; v = (Vector*)malloc(sizeof(Vector)); v->size = 0; v->capacity=16; v->array=(Type*)realloc(NULL, sizeof(Vector)*(v->capacity *= 2)); return v; } void vec_add(Vector *v, Type value){ v->array[v->size] = value; if(++v->size == v->capacity) v->array=(Type*)realloc(v->array, sizeof(Vector)*(v->capacity *= 2)); } size_t vec_size(Vector *v){ return v->size; } Type *vec_getArray(Vector *v){ return v->array; } void vec_free(Vector *v){ free(v->array); free(v); } int cmp(const void *a, const void *b){ return strcmp(*(char**)a, *(char**)b); } void merge(const char *inpfile1, const char *inpfile2, const char *outfile){ FILE *fp; char buff[256], **array; Vector *v; size_t c, size; v = vec_make(); fp=fopen(inpfile1, "r"); while(NULL!=fgets (buff, 256, fp)) vec_add(v, strdup(buff)); fclose(fp); fp=fopen(inpfile2, "r"); while(NULL!=fgets (buff, 256, fp)) vec_add(v, strdup(buff)); fclose(fp); array=vec_getArray(v); size=vec_size(v); // qsort(array, size, sizeof(Type), cmp); fp=fopen(outfile, "w"); for(c=0;c < size; ++c){ fputs(array[c], fp); free(array[c]); } fclose(fp); vec_free(v); } int main(){ merge("book1.txt", "book2.txt", "fixed.txt"); return 0; }
这篇关于存储从一个文件中的字符串到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!