如何访问一个while循环外阵列 [英] how can i access arrays outside a while loop

查看:83
本文介绍了如何访问一个while循环外阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何可以访问哪些被填充while循环内循环外的数组?

how can I access an array outside a loop which was populated inside a while loop ?

MAIN.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);      
}

Readline.c

Readline.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循环中,但外循环,如果那么他们只显示1号线的60倍。

whats the easiest way to pass the four arrays created in the while loop to threads as arguments? I every time I try to print out the arrays outside of the while loop there is nothing and when I try to print them out inside the while loop but outside the if loops then they only display the 1st line 60 times.

推荐答案

您必须将指针传递给数组READLINE ...这是很好的做法传递的最大长度,也避免缓冲区溢出错误。

You must pass the pointer to the array to ReadLine... It's good practice to pass the maximum length, too, to avoid buffer overflows errors.

#define MAXLEN 240

char Line[MAXLEN+1];

while ((LineLength = Readline(filep, Line, MAXLEN))!=0)
{
  ...
}


int Readline(FILE *filep, char *Line, int maxlen)
{
   ...

这篇关于如何访问一个while循环外阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆