#include "Definition.h"
#include <stdio.h>
#include "ExternalVar.h"
#include <stdlib.h>
#include <string.h>

extern int Readline(),CountWord(),CountsUpdate();


char Line[MaxLine];  /* one line from the file */


int NChars = 0,  /* number of characters seen so far */
    NWords = 0,  /* number of words seen so far */
    NLines = 0,  /* number of lines seen so far */
    LineLength;  /* length of the current line */

int wc = 0,
    lc = 0,
    cc = 0,
    tc = 0;


int i;

main(int argc, char *argv[])
{
    FILE *fp;
    fp= fopen(argv[1],"r");
    if (fp)
    {
        while (i=fscanf(fp,"%s",Line)!=EOF)
        ///printf("%s \n",Line);
        cc=Readline(Line);
        printf("%d \n",cc);
        fclose(fp);


    }
    return 0;
}


这是我的主要功能,正在将char数组Line []传递给函数Readline(Line),我希望它返回数组中的char数。

这是功能Readline.c。无论我使用哪个文本文件作为参数,我都将继续获得84作为返回值。由于我是C编程新手,因此不确定自己在做错什么或做对了。请帮忙。

#include "Definition.h"
#include "ExternalVar.h"
#include <stdio.h>



int Readline(char *Line)
{
int i;


for (i = 0; Line[i] == '\n' ; i++)
{

return i;
}
}


输出如下

taj @ taj:〜/ Desktop / 2014_Summer_CIS5027_Asg2 $ gcc Main.c Readline.c
taj @ taj:〜/ Desktop / 2014_Summer_CIS5027_Asg2 $ ./a.out b.txt

84

最佳答案

int Readline(char *Line){
    int i;
    for (i = 0; Line[i] != '\0' ; i++)//newline is not included in the Line. Because read by fscanf(fp,"%s",Line)
        ;

    return i;
}

关于c - 我如何遍历传递给函数的char数组并返回char数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24344958/

10-12 07:35