簡體   English   中英

有沒有辦法從文件中獲取文本並將其轉換為我的 c 程序的參數?

[英]Is there a way to take text from a file and turn it into an argument for my c program?

我有一個 C 項目,用戶可以在其中輸入 8 位二進制數或打開一個帶有 8 位二進制數的文本文件。 我遇到的問題是從文件中獲取 8 位二進制數並將其作為我的程序的參數 char* argv[] 。 這就是我從文件中讀取 8 位二進制數的方式

    fd = open(argv[1], O_RDONLY);
    read(fd,jString, 100);
    close(fd);
    printf("jString: %s\n", jString);

現在我只知道如何將 8 位二進制數放入一個字符串中,但我想要的是將文件中由空格分隔的每個 8 位二進制數輸入轉換為我的程序的參數 char* argv[] 如果這是可能的那我該怎么做呢?

我已經嘗試過的東西是這個。

    fd = open(argv[1], O_RDONLY);
    read(fd,argv[1], 100);

這樣做的問題是它只是將整個文件讀入 argv[1] 並且沒有將它們用空格和我的程序的不同參數分開。

我遇到的問題是從文件中獲取 8 位二進制數並將其作為我的程序的參數 char* argv[]

你不可以做這個。 通常的聲明

int main(int argc, char** argv){};

意味着這兩個參數是在您的程序運行時由操作系統為您提供的。 你可以做的是為你的程序構建這樣的東西,假設一個int myArgCountchar** myArgValues; 但似乎你不需要那個。

如果task是你的程序,運行task 00001111會將00001111放入argv[1] 並將argc設置為 2,因為argv[0]始終是程序的完整路徑。

起初你說可以在文件中提供二進制數,但在下一段中你說

我想要的是將每個 8 位二進制數輸入以空格分隔

如果實際上輸入文件可以有一個由空格分隔的 8 位二進制數的列表 --- 而不僅僅是一個 --- 你將需要像系統為你做的那樣構建一個列表,為數字分配內存和創建一個指向它們的指針數組,以及一個包含數字計數的 int。 這並不復雜。

下面的代碼測試命令行上的參數,如果不存在,則嘗試打開source.txt文件以獲取一個。 可能會有所幫助。

注意使用scanf()從文件中讀取值。 使用的掩碼"%8[01]"非常方便:它只接受 0 和 1,最多 8 位。

#define _CRT_SECURE_NO_WARNINGS
#include "stdio.h"
#include "stdlib.h"
#include "string.h"

int main(int argc, char** argv)
{
    char        binary_number[10];
    char*       mask = "%8[01]"; // for scanf()
    const char* FileName = "source.txt";

    if (argc < 2)
    {   // no number on the command line
        fprintf(stderr, "\nNo 8-bit number provided on the command line\n");
        FILE* in_file = fopen(FileName, "r");
        if (in_file == NULL)
        {
            fprintf(stderr, "Could not open [%s]\n", FileName);
            return -1;
        }
        int n = fscanf(in_file, mask, binary_number);
        printf("\nFrom the file [%s] number is [%s]\n", FileName, binary_number);
        fclose(in_file);
    }
    else
    {   // number provided
        strncpy(binary_number, argv[1], 9);
        fprintf(stderr, "\nFrom the command line: [%s]\n", argv[1]);
    };
    return(EXIT_SUCCESS);
};

該節目顯示

From the command line: [101010]

或者

    No 8-bit number provided on the command line
    From the file [source.txt] number is [11110000]

這里你的程序具有動態內存分配:

  • 參數分隔符 = ' '
  • 來自命令的參數 = your_program_name "10001010" "01010101"
  • 來自文件的參數:命令行 = your_program_name "...\\yourfile.xyz"示例:

在此處輸入圖片說明

#include <malloc.h>
#include <string.h>
#include <stdio.h>

#define DELIMITER ' '

// Tokenize function
int Tokenize(char* pcLine, char ** pcListeArgs, const char* pcDelim)
{
    int iNumElet = 0;
    size_t len = 0;         

    // Get line size
    int init_size = strlen(pcLine);

    char *ptr = strtok(pcLine, pcDelim);

    while(ptr != NULL)
    {
        len = strlen(ptr);
        pcListeArgs [iNumElet] = (char*) calloc (len+1, sizeof (char));
        memset(pcListeArgs [iNumElet], 0, len+1); // reset content
        memcpy (pcListeArgs [iNumElet], ptr, len); // copy data

        ptr = strtok(NULL, pcDelim);

        iNumElet ++;
    }

    return iNumElet;
}

int main(int argc, char** argv)
{
    FILE * fp;
    char * line = NULL;
    size_t len = 0;
    char ch;
    char *pcFileContent;
    char cDelim = DELIMITER;



    // Case : argument are a file 
    if (argc == 2) 
    {
        const char* pcFilePath = argv[1];
        fp = fopen(pcFilePath, "r");

        if (fp == NULL)
        {
            perror("Error while opening the file.\n");
            return (-1);
        }

        // Put the curso at the end of file
        fseek (fp,0, SEEK_END);

        // Get number of charactes
        len = ftell(fp);

        // Allocat memory 
        pcFileContent = (char*) calloc (len+1, sizeof (char));

        //Erase content to zero
        memset(pcFileContent,'\0',len+1);

        // Put the curso at the begining of file
        fseek (fp,0, SEEK_SET);

        // Read file char by char
        unsigned int i=0;
        unsigned int iNbArgs = 1;

        while((ch = fgetc(fp)) != EOF)
        {
            if (ch == DELIMITER)
                iNbArgs ++; // We have a new arguments              

            pcFileContent[i++] = ch;
        }

        char **pcListeArgs =  (char**) calloc (iNbArgs, sizeof (char*));

        iNbArgs = Tokenize (pcFileContent, pcListeArgs, &cDelim);

        // here you have all you arguments ....
        for ( i=0; i< iNbArgs; i++)
        {
            printf ("Argument %d = %s\n", i, pcListeArgs[i]);
        }

        // Free memory
        for(int i = 0; i < iNbArgs; i++)
            free(pcListeArgs[i]);

        free(pcListeArgs);
        free(pcFileContent);

    }else if ( argc > 2) // Case : argument is binary number
    {
        for ( int i=1; i< argc; i++)
        {
            printf ("Argument %d = %s\n", i, argv[i]);
        }
    }

    return 0;
}

結果:

  • 來自命令的參數 = ArgParsor.exe "10001010" "01010101" "01010111"

在此處輸入圖片說明

  • 來自文件的參數:命令行 = ArgParsor.exe "G:\\temp.txt"

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM