[英]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)如果第一个字符
\\n
的Line
? 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()函数应返回一个
int
: int 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.