[英]C to java file reading and printing
我有一個C代碼,其輸出如下:
輸入: ./includeCrawler -Itest s_01.c
輸出: s_01.o: s_01.c i_60.h i_44.h i_46.h i_04.h i_51.h i_15.h i_33.h i_29.h i_16.h
S_01.c:
#include "i_60.h"
#include "i_44.h"
#include "i_46.h"
#include "i_04.h"
#include "i_51.h"
#include "i_15.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
#include <sys/time.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <signal.h>
i_60.c
#ifndef _I_60_H_
#define _I_60_H_
#include "i_33.h"
#include "i_04.h"
#include "i_29.h"
#include <stdio.h>
#endif /* _I_60_H_ */
我試圖將其轉換為java,從而它讀取例如s_01.c的文件,並將輸出上述輸出。 文件本身將讀取所有ih並將其輸出。 如果存在依賴性,例如程序讀取s_01.c,它將讀取s_01.c中的文件,並且將讀取其中的#include“ i_60.h”,然后將其轉到i_60.h,並找到更多與i相關的東西。它會繼續這樣做,直到找不到我的路。
這是我到目前為止所做的事情:我已經成功讀取了例如1個文件,並打印了與i相關的所有內容,然后將其扔到了鏈表中。但是,我不知道如何繼續互相調用文件並然后打印輸出。
read: s_02.c
Output:
[i_02.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h]
[i_02.h, i_52.h, i_51.h, i_03.h]
[i_02.h, i_52.h, i_51.h, i_03.h, i_41.h]
好的,我假設您已經實現了一個名為readFile(String filename)的函數,它將返回其依賴的文件名數組(或您所說的鏈表)。
您可以使用遞歸過程。 順便說一句,您可以使用Set來保存文件名,而不是linkedList。
// a set of filenames that have been read
HashSet<String> setOfProceededFilenames = new HashSet<String> ();
// recursively read the files
private void recursivelyReadFiles(String filename) {
// we have defined String [] readFile(String filename)
String [] dependencies = readFile(filename);
// we only read those haven't been proceeded
for (String dependentFile : dependencies) {
if (!setOfProceededFilenames.contains(dependentFile)){
// add this file to the set and read its dependencies
setOfProceededFilenames.add(dependentFile);
recursivelyReadFiles(dependentFile);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.