我如何访问在 while 循环内填充的循环外的数组?
主.c
#define _CRT_SECURE_NO_WARNINGS
#include "Definition.h"
#include <stdio.h>
#include <string.h>
#include <pthread.h>
extern int Readline(),CountWord();
char Line[500]; /* one line from the file */
char myFileList[300][MaxLine];
char myFileList2[300][MaxLine];
char myFileList3[300][MaxLine];
char myFileList4[300][MaxLine];
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 */
void *ThreadTask(void *threadarg)
{
//receive array
//match each element with file name in directory
//perform counts on each file
//print store or return values
//printf("%s",myFilesList);
}
FILE *filep;
int main(int argc, char **argv)
{
int i = 0;
filep = fopen(argv[1], "r");
if (!filep){
printf("No %s such file found\n", argv[1]);
return -1;
}
while ((LineLength = Readline(filep))!=0) //here in this loop I split a big file into 4 arrays depending on the number of lines(240). How can I access these newly created arrays outside the while loops. I need to pass them to individual threads as arguments.
{
if(i>=0 && i<60){
strcpy(myFileList[i], Line);
printf("%s\n", myFileList[i]);
i++;
}
if(i>=60 && i<120){
strcpy(myFileList2[i], Line);
//printf("%s\n", myFileList2[i]);
i++;}
if(i>=120 && i<180){
strcpy(myFileList3[i], Line);
//printf("%s\n", myFileList3[i]);
i++;}
if(i>=180){
strcpy(myFileList4[i], Line);
//printf("%s\n", myFileList4[i]);
i++;}
}
fclose(filep);
}
阅读线.c
#include "Definition.h"
#include "ExternalVar.h"
#include <stdio.h>
int Readline(FILE *filep)
{
//Please implement your code here
int i = 0;
char ch;
while (!feof(filep) && i<MaxLine){
ch = fgetc(filep);
if (ch == '\n'){
break;
}
Line[i] = ch;
i++;
}
Line[i] = 0;
return i;
}
将 while 循环中创建的四个数组作为参数传递给线程的最简单方法是什么?我每次尝试在 while 循环外打印数组时,什么也没有,当我尝试在 while 循环内但在 if 循环外打印它们时,它们只显示第一行 60 次。
最佳答案
如果使用 PThreads,请使用 pthread_create()
的最后一个参数将参数传递给 tread 函数。
关于c - 我如何在 while 循环之外访问数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24427076/