c - 用文件 txt 中的单词填充字符串数组

标签 c arrays string file

我试图用文件的每个单词填充数组的每一行。 我不想过度分配内存,所以我想至少知道最长单词的长度和我应该分配的行数,以及文件中写入的单词数。 我无法理解代码中的问题出在哪里。我认为计算最长单词应该是一个问题,因为当我在分配它打印的函数返回的值后打印longest_file_word时,它会打印-1。

显然这不起作用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

int longestWord(char *file, int *nWords);
char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen);

int main(int argc, char *argv[]){
    int  file_elements_number=0 , i , j , k , z , longest_file_word , count_file_words ;
    char *filename =(char*)malloc((strlen(argv[2]) +1 )*sizeof(filename));
    strcpy( filename , argv[1]);
    for(i  =  0; i < strlen(argv[1])+1 ; i++){
        printf("%c" , filename[i]);
    }
    if(filename = NULL){
        printf("Non c'e' abbastanza memoria");
        return 1;
    }
    if(argc!=2)
    {
        printf("Errore numero parametri passati da linea di comando\n");
        return 1;
    } 
    longest = longestWord( filename , &count);
    printf("ciao %d\n%d\n", count , longest);
    char **file_words = Create2DStr(count, longest);
    FILE *file_ptr;

    const char delim[] = {" \n\t"};
    char line[260];
    char *buf = NULL;

    file_ptr = fopen( filename, "r");
    count=0;
    while(fgets(line, 260, file_ptr))
    {
        buf = strtok(line, delim);
        while(buf)
        {    
            if((strlen(buf) > 0)){
                strcpy(file_words[count], buf);
                count++;
            }
            buf = strtok(NULL, delim);
        }
    }
    for(i = 0 ; i < count ; i++){
         for( j = 0 ; j < longest ; j++){
             printf("%c" , file_words[i][j]);
         }
         printf("\n");
    }


    fclose(file_ptr);    
    free(filename);
    filename = NULL;
    return 0;


}






int longestWord(char *filename, int *nWords)
{
    FILE *file_ptr=0;
    int cnt=0, longest=0, numWords=0;
    char c;
    file_ptr = fopen(filename, "r");
    if(file_ptr){
        while ( (c = fgetc(file_ptr) ) != EOF )
        {
            if ( isalnum (c) ) {
                cnt++;  
            }
            else if ( ( ispunct (c) ) || ( isspace(c) ) || (c == '\0' ) || (c== '\n'))
            {
                (cnt > longest) ? (longest = cnt, cnt=0) : (cnt=0);
                numWords++;
            }
        }
        *nWords = numWords;
        fclose(file_ptr);
    }
    else {
        return -1;
    }

    return longest;
}

char ** Create2DStr(ssize_t numStrings, ssize_t maxStrLen){
    int i;
    char **a = {0};
    a =(char**) calloc(numStrings, sizeof(a));
    for(i=0;i<numStrings; i++)
    {
        a[i] = (char*)calloc(maxStrLen + 1, 1);
    }
    return a;
}

最佳答案

你正在做,

if(filename = NULL)

而不是

if(filename == NULL)

读取你的文件名后。

您应该在 gcc 上打开警告 -Wall 的情况下进行编译。

关于c - 用文件 txt 中的单词填充字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53668259/

相关文章:

c - 使用子进程处理信号 SIGSTP、SIGCONT、SIGINT

python - 在 float numpy 数组中打印字符

java - 如何在 Java 中将字符串子串到第二个点 (.)?

c - OpenCV 'cvQueryFrame' 在 OS X 上非阻塞?

c - c语言逻辑运算符的误解

arrays - 为Kotlin中的每个元素增值

javascript - 隐藏 CanvasJS 图中的所有系列按钮(javascript foreach 用法)

java - Java中交换字符串内字符的API方法

java - 如何确保字符串具有特定格式

c - 变量类型本身就是变量的语言?