c、二维字符数组和fopen

标签 c arrays file pointers fopen

我正在尝试编写一个程序来读取包含名称列表的文件。这些名称的数量可以变化,名称长度也可以变化。我想将它们存储在 char 数组的数组中,并将每一行作为字符串读取,以便稍后打开与相关名称对应的文件。但是当我尝试打开第一个时,我在打开文件时出错。 我完全没有想法。 帮忙,好吗?

这是与此操作相关的代码:

#include <stdio.h> 
#include <stdlib.h>
#include <math.h>
#include <string.h>
int glimps(char *fname);
int write_av(char *fname, int NumbFiles);
int clr(char *fname);
int readFile(char *fname, int i);

double *dalpha, *alpha, *Ln_t, *LnLnA, Conc;
long *time, *weights, *Lmax, Nmax;
char *av_file, **in_files, *antetka;

/****************************************************************************/

int main(int argc, char *farg[])
{
 int i, NumbFiles, flag;
 long row;
 char *a1;
 FILE *fp;

 av_file = farg[1];
 printf("av_file = %s\n",av_file);
 NumbFiles = glimps(av_file);

 in_files = (char **) malloc (sizeof(char *) * NumbFiles);
 for (i=0 ; i<NumbFiles ; i++) 
      in_files[i] = (char *) malloc (sizeof(char) * 200);

 Lmax = (long *) calloc((size_t) NumbFiles, sizeof(long));
 if((in_files == NULL)||(Lmax==NULL))  printf("Грешка при read алок.\n, "), exit(-1);     
 if (flag = readFile(av_file, -1)) 
     printf("Error in read av_file %s\n", av_file), exit(-1);
 weights = (long *) calloc((size_t) Nmax, sizeof(long));
 for(i = 0; i<Nmax; i++) weights = 0;
 for(i = 0; i<NumbFiles; i++)
 {
       //if (flag = readFile(&(*in_files[i]), i)) 
       if (flag = readFile(in_files[i], i))
           printf("Error in in_files[%d], %s\n",i, &(*in_files[i])), exit(-1);     
 }
 if (flag = write_av(av_file, NumbFiles)) 
     printf("Error in write_av(%s)\n,", av_file), exit(-1);      
 exit(0);
}

/****************************************************************************/

int glimps(char *fname)
{
 FILE *fp;
 char buf[140];
 int cnt=0;

 fp = fopen (fname, "r");
 while (fgets(buf,140,fp) )
 {
        cnt++;
 }
 fclose(fp);
 return (cnt);
}
/****************************************************************************/

int readFile(char *fname, int k)
{
 int cnt=0;
 FILE *fp;
 char buf[200], dummy[13];

 printf("fname is %s\n", fname); getchar();
 fp = fopen (fname, "r");
 if(fp==(NULL)) return(-1);
 if(!strcmp(fname,av_file) )
 {
    while (fgets(in_files[cnt++],200,fp) );
 }
 else
 {
    printf("read TUK!\n"); getchar();

    fgets(buf,200,fp);
    sscanf(buf,"%s %s %s %s %s %s %s %ld %s %s %lf\n", 
                dummy, dummy,dummy,dummy,dummy,dummy,dummy, &Lmax[k], 
                dummy, dummy, &Conc);           
    fgets(buf,200,fp);
    sscanf(buf,"%s\n", antetka);        
    printf("read TUK!\n"); getchar();
    while (fgets(buf,200,fp))
    {
           sscanf(buf,"%ld %lf %lf %s %lf %lf\n", 
                  &time[cnt], &dalpha[cnt], &alpha[cnt], dummy, &Ln_t[cnt], 
                  &LnLnA[cnt]);
           weights[cnt++]++;
    }

 }
 fclose(fp);
 return (0);
}

...

控制台输出:

> ./avr alpha_cubeL.C0.010
av_file = alpha_cubeL.C0.010
fname is alpha_cubeL.C0.010
fname is alpha_cubeL100C0.010
Error in read in_files[0], alpha_cubeL100C0.010

> ls alpha_cubeL100C0.010
alpha_cubeL100C0.010

最佳答案

发生的事情是,在 readFile 函数中,您读取作为参数给出的主文件,以便(从内容中)在 in_files[i] 中制作几个文件名,但是 fgets 读取包含 CR 或 CRLF(即行尾字符)的行。因此,稍后在程序中,readFile 在尝试打开 filename + CR [LF] 时失败。

您可以在程序顶部附近添加一个trim 函数,例如

void trim(char *s) {
  int i,l = strlen(s);
  for (i=l-1 ; i>=0 && (s[i]==10 || s[i]==13) ; i--) s[i] = 0;
}

删除以字符串 s 结尾的 CR 和/或 LF,然后将 readFile 函数更改为 trim 读入的文件名每一行,就像

  while (fgets(in_files[cnt++],200,fp) ) {
    trim(in_files[cnt-1]); // cnt-1, or do the cnt++ here (and not above...)
  }

然后文件就可以打开了...

(这可能不是这个程序中唯一的问题,但这是一个好的开始)

关于c、二维字符数组和fopen,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15585014/

相关文章:

c# - 使用多维数组索引器的最有效方法

javascript - new Array() 创建的 <empty> 元素有什么值?

c - 从 C 中的单个格式化键盘行填充数组

arrays - Matlab 最后一维访问 ndimensions 矩阵

c - 在c结构数组中显示值

python - Selenium 在下载时给出文件名

Python - 根据第一个单词对文件中的行进行排序?

android - 如何在Android中获取子字符串?

c - C编译器在递归过程中如何处理堆栈中的局部变量?

c - 实现简单链表