捕获命令行参数并存储它

时间:2016-04-14 09:05:13

标签: c command-line-arguments

我正在尝试捕获命令行参数并将其存储为字符串但是当我尝试将其转换为int时出现编译器错误:

  

错误是:初始化从没有强制转换的指针生成整数   [默认启用]        char test = argv [1];

import java.io.*;
import java.util.Scanner;


public class TextFileProcessor
{
public static void textFile()
{
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Test Input File:");
    String inputFile = keyboard.next();
    System.out.print("Output File:");
    String outputFile = keyboard.next();

    try
    {
        BufferedReader inputStream = new BufferedReader(new FileReader(inputFile));
        PrintWriter outputStream = new PrintWriter(new FileOutputStream(outputFile));
        String line = inputStream.readLine();

        line = line.replaceAll("\\s+", " ").trim();
        line = line.substring(0,1).toUpperCase() + line.substring(1);
        //This is where I would like to add code

        while(line != null)
        {
            outputStream.println(line);
            System.out.println(line);
            line = inputStream.readLine();
        }

        inputStream.close();
        outputStream.close();
    }

    catch(FileNotFoundException e)
    {
        System.out.println("File" + inputFile + " not found");
    }
    catch(IOException e)
    {
        System.out.println("Error reading from file" + inputFile);
    }
}
}

1 个答案:

答案 0 :(得分:5)

argv[1]持有一个字符串,你试图将它存储在字符变量中,你需要的是一个字符指针

char *test = argv[1];

并打印

printf("%s\n", test);