首页 > 解决方案 > 将输入文件重定向到程序,只正确读取一个命令

问题描述

我的程序运行正常,如果我手动输入任何命令,它也会正确读取所有内容并将其设置为正确的变量,然后将其输出到二进制文件。

但是,当我在命令行中重定向输入文件时,它只会读取用于开关的命令,但不会正确读取其他任何内容。

例如

a7 < a7Input.txt

输入文本示例

r
1023
r
4393
c
3423
Systems Programming
MWF
3
68
c
3421
Systems Programming Recitation
TR
1
68

Enter one of the following actions or press CTRL-D to exit
C - Create a new course record
R - Read an existing course record
U - Update an existing course record
D - Delete an existing course record
This is Choice R
Please Enter a Course Number to Display
courseNumber:    0
courseName:      e record
courseSched:     urscourseName:  0
courseSize:      -1630458206

YOU PICKED C for Create
Please Enter The Details of The Course
Enter a course number:
Enter the course schedule (MWF or TR):
Enter a Course Name:
Enter the course credit hours:
Enter Number of Students Enrolled:
Course Has Been Created!

请注意,它不会从文件中填充任何内容,而且我不完全确定当我手动输入内容时会发生什么,它是否可以正常工作。

这是我的代码供参考

#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdarg.h>


typedef  struct{
    char  courseName[64];
    char  courseSched[4];
    unsigned  int  courseHours;
    unsigned  int  courseSize;
} COURSE;

/* files */
FILE *pfileInputFile;
FILE *pFileDirect;

void proccessInputCommands(char *pszInputFile, char *pszDirectFile, char *argv[]);
void getFileNames(int argc, char *argv[], char **ppszDirectFileName, char **ppszInputTxtFileName);


int main(int argc, char *argv[])
{
    char *pszDirectFile = NULL;
    char *pszInputFile = NULL;
    int rc;
    int iCommandType;   //Type of command

    /* get the filenames and comand type from the command switches */
    getFileNames(argc, argv, &pszDirectFile, &pszInputFile);

    proccessInputCommands(pszInputFile, pszDirectFile, argv);
    return 0;
}

void proccessInputCommands(char *pszInputFile, char *pszDirectFile, char *argv[])
{
    COURSE course;
    char szInputBuffer[100];
    char cCommand;
    long lRBA;
    long lCourseNum;
    char szRemaining[100];
    int iScanfCnt;
    int iWriteNew;
    int rcFseek;
    int rc;

    // open the txt file for read
    pfileInputFile = fopen(pszInputFile, "r");
    if(pfileInputFile == NULL)
        printf("ERROR\n");

    // open the new direct data file for write binary
    // if it already exists we simply update it
    pFileDirect = fopen(pszDirectFile, "wb+");
    if(pFileDirect == NULL)
        printf("ERROR\n");

    //get commands until EOF
    //fgets returns null when EOF is reached

    while(fgets(szInputBuffer, 100, pfileInputFile) != NULL)
    {
        if(szInputBuffer[0] == '\n')
            continue;
        printf("> %s",szInputBuffer);
        iScanfCnt = sscanf(szInputBuffer, "%c\n %ld\n %99[^\n]\n"
            , &cCommand
            , &lCourseNum
            , szRemaining);
        if(iScanfCnt < 2)
        {
            printf("Error: Expected command and Course Number, found: %s\n"
                , szInputBuffer);
            continue;
        }
        switch(cCommand)
        {
            case 'c':
            case 'C':
                iScanfCnt = sscanf(szRemaining, "%64[^\n] %4s %d %d"
            , course.courseName
            , course.courseSched
            , &course.courseHours
            , &course.courseSize);

            // check for bad input.
            if(iScanfCnt < 4)
                printf("ERROR\n");
            lRBA = lCourseNum*sizeof(COURSE);
            rcFseek = fseek(pFileDirect, lRBA, SEEK_SET);
            assert(rcFseek == 0);
            // write it to the direct file
            iWriteNew = fwrite(&course
                , sizeof(COURSE)
                , 1L
                , pFileDirect);
            assert(iWriteNew == 1);
            break;
            case 'r':
            case 'R':
                lRBA = lCourseNum*sizeof(COURSE);
                rcFseek = fseek(pFileDirect, lRBA, SEEK_SET);
                assert(rcFseek == 0);
                //print the informatio at the RBA
                rc = fread(&course, sizeof(course), 1L, pFileDirect);
                if(rc == 1)
                    printf("%-64s %-4s %5d %5d\n"
                        , course.courseName
                        , course.courseSched
                        , course.courseHours
                        , course.courseSize);
                else
                    printf("Course Number %ld not found for CBA %ld\n",lCourseNum, lRBA);
                    break;
                default:
                printf("unknown command\n");
        }
    }
    //close the file 
    fclose(pfileInputFile);
    fclose(pFileDirect);
}

void getFileNames(int argc, char *argv[], char **ppszDirectFileName, char **ppszInputTxtFileName)
{
    int i;

    // If there aren't any arguments, show the usage
    if (argc <= 1)
        printf("No Arguments \n");

    // Examine each of the command arguments other than the name of the program.
    for (i = 1; i < argc; i++)
    {
        if(i == 1)
        {
            *ppszInputTxtFileName = argv[i];
        }
        if(i == 2)
        {
            *ppszDirectFileName = argv[i];
        }
    }
}

标签: cbinary

解决方案


推荐阅读