有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

C到java文件读取和打印

我有一个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,通过它读取eg s_01的文件。c,它将打印上述输出。文件本身将读取所有i.h并输出它。如果存在依赖项,例如程序是否读取s_01。c它将读取s_01中的文件。它会读#包括“i#u 60.h”,然后它会进入i#u 60。我发现更多相关的东西。。它将继续这样做,直到它再也找不到我的路径

以下是我迄今为止所做的工作: 我已经成功地阅读了eg 1文件并打印出了所有与我有关的内容,然后我把它放到了linkedlist中。。但是,我不知道如何继续调用其他文件,然后打印输出

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]

共 (1) 个答案

  1. # 1 楼答案

    好的,我假设您已经实现了一个名为readFile(stringfilename)的函数,它将返回一个它所依赖的文件名数组(或者可能是一个linkedList)

    您可以使用递归过程。顺便说一句,您可以使用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);
            }
        }
    }