简体   繁体   English

我如何遍历传递给函数的char数组并返回char数

[英]how do i loop through a char array being passed to a function and return number of chars

#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;  
}

This is my main function I am passing the char array Line[] to the function Readline(Line) and I want it to return the number of chars in the array. 这是我的主要功能,正在将char数组Line []传递给函数Readline(Line),我希望它返回数组中的char数。

Here is the function Readline.c . 这是功能Readline.c。 No matter which text file I use as the argument I keep getting 84 as the returning value. 无论我使用哪个文本文件作为参数,我都将继续获得84作为返回值。 Not sure what I am doing wrong or right since I am new to C programming. 由于我是C编程新手,所以不确定自己在做错什么或做对了。 Please help. 请帮忙。

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



int Readline(char *Line)
{   
int i;


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

return i;
}
}

Output is as folllows 输出如下

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

84 84

You have few problems: 您有几个问题:

1) You probably want to check until you hit \\n . 1)您可能要检查,直到您按下\\n为止。 So the condition should really be: 因此条件应该确实是:

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

2) Your input may not contain \\n . 2)您输入的内容可能不包含\\n So you should also check whether you have reached the end of the string: 因此,您还应该检查是否已到达字符串的末尾:

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

3) What if the first character is \\n in Line ? 3)如果第一个字符\\nLine It won't enter the loop at all and you are not returning anything in that case. 它根本不会进入循环,在这种情况下您将不会返回任何东西。 So you should initialize i=0 and return it. 因此,您应该初始化i=0并返回它。 You could rewrite the function as: 您可以将函数重写为:

int Readline(char *Line)
{   
int i = 0;

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

return i;
}

4) fscanf() with %s format doesn't read \\n even if your input file contains it. 4)即使输入文件中包含%s格式的fscanf()也不读取\\n You probably want to use fgets() to read which does read the \\n chars: 您可能想使用fgets()来读取哪个\\n字符:

while (fgets(Line, sizeof Line, fp)) {
  ...
}

5) main() function should return an int : int main(int argc, char *argv[]) {... } 5)main()函数应返回一个intint main(int argc, char *argv[]) {... }

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用 char** ? (指向字符数组的指针) - How do I use char** ? (pointer to an array of chars) 如何将字符数组传递给函数? - How do I pass an array of chars to a function? 读通过以指针增量作为void *传递的char数组,然后读为char和其他数据类型? - Reading through an char array passed as void* with pointer incrementation and later read as chars and other datatypes? 如何从函数返回字符数组? 然后在另一个函数中使用返回的char? - How do I return a character array from a function? Then use the returned char in another function? C 字符数组在传递到 function 后损坏 - C char array getting corrupted after being passed into function 如何在C中使用char *来遍历字符? - How do I loop through characters with char*'s in C? 递归函数返回char数组中的数字 - Recursive function to return number in char array 如何在不扫描enter /空格字符的情况下扫描到2d字符数组? - How do I scan into a 2d char array without scanning in the enter/space chars? 如何将字符数组与 C++ 中的字符进行比较? 为什么不工作? - How do I compare a chars array with a char in c++? Why isn't working so? 无法取消引用通过 function 传递的 char* 数组 - Can't dereference a char* array passed through a function
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM