2014-02-11 59 views
1

因此,我正在讀取文件並使用系統調用將文件內容輸出到控制檯。另外我想爲輸出添加一個空間,每遇到20條線。這是我遇到麻煩,儘管下面的代碼幾行,正在顯示的整個文件不含空格系統調用:輸出文件並暫停每20行

 //Write file contenst 
    while((nReadFile = read(nOpenFile, buffer, 1) != 0)) 
{ 
    write(1, &nLineCount, sizeof(nLineCount)); 

    if(nLineCount == 20) 
    { 
     write(1, "\n", 2); 
     nLineCount = 0; 
    } 

    if(nReadFile = write(1, buffer, nReadFile) == '\n') 
    { 
     nLineCount++; 
    } 

} 

這裏是整個程序(不包括含庫.h文件)

#include"mymore.h" 

int main(int argCount, char *argv[]) 
{ 

    struct termios initial_settings, new_settings; 

    tcgetattr(fileno(stdin), &initial_settings); 
    new_settings=initial_settings; 
    new_settings.c_lflag &= ~ICANON; 
    new_settings.c_lflag &= ~ECHO; 
    new_settings.c_cc[VMIN] = 1; 
    new_settings.c_cc[VTIME] = 0; 
    if (tcsetattr(fileno(stdin), TCSANOW, &new_settings)!=0) 
    { 
     fprintf(stderr, "could not set attributes\n"); 
    } 

    int nLineCount = 0; 

    int nCheckFile = 0; 
    int nFileCountCounter = 1; //first arguement interested in is argv[1] 
    int nOpenFile = 0; 
    int nReadFile = 0; 
    int nCount = 0; 
    char *cData; 
    char buffer[0]; 

    //check that arguements have been provided 
    if(argCount < 2) 
    { 
     write(1, "There needs to be at least one file provided! \n", 50); 
     return 1; 
    } 

    do 
    { 
    printf("%d", argCount); 
    //check if file exists 
    nCheckFile = access(argv[nFileCountCounter], F_OK); 

    if(nCheckFile != 0) //if file does not exist 
{ 
    write(1, "The file ", 10); 
    write(1, argv[nFileCountCounter], strlen(argv[1])); 
    write(1," does not exist! \n", 25); 
    return 1; 
} 
    else //file does exist 
{ 
    write(1, "Opening ", 10); 
    write(1, argv[nFileCountCounter], strlen(argv[1])); 
    write(1, "\n", 2); 
} 

    //open the file 
    nOpenFile = open(argv[nFileCountCounter], O_RDONLY); 

    if(nOpenFile < 0) 
{ 
    write(1, "Failed to open file ", 10); 
    write(1, argv[nFileCountCounter], strlen(argv[nFileCountCounter])); 
    write(1, "\n", 2); 
    return 1; 
} 

    //read file 
    cData = (char *) malloc(100 * sizeof(char)); 



    cData[nReadFile] = '\0';//append null terminator 

    //find length of source file 
    while(cData[nCount] != 0) 
{ 
    nCount++; 
} 

    //Write file contenst 
    while((nReadFile = read(nOpenFile, buffer, 1) != 0)) 
{ 
    write(1, &nLineCount, sizeof(nLineCount)); 

    if(nLineCount == 20) 
    { 
     write(1, "\n", 2); 
     nLineCount = 0; 
    } 

    if(nReadFile = write(1, buffer, nReadFile) == '\n') 
    { 
     nLineCount++; 
    } 

} 

    cout << nLineCount << endl; 
    //close file 
    close(nOpenFile); 

    //Increment to next file 
    nFileCountCounter++; 

}while(nFileCountCounter < argCount);//while there are still arguements 

    tcsetattr(fileno(stdin), TCSANOW, &initial_settings); 
    return 0; 
} 

這實際上是使用系統調用我的第一次的經驗,並有一兩件事我想我已經注意到的是,被之前的任何的C代碼執行的寫命令?

任何想法? 感謝

+1

這不是C++。 –

回答

0

有很多在代碼中的錯誤:

  1. char buffer[0];應該char buffer[1];

    char buffer[0];定義buffer可存儲的性格,我想這是不是你想要的。

  2. write(1, "There needs to be at least one file provided! \n", 50);應該是

    char *msg = "There needs to be at least one file provided! \n"; 
    write(1, msg, strlen(msg)); 
    

    消息長於50有相似藪錯誤,並且可以類似地被固定。

  3. while((nReadFile = read(nOpenFile, buffer, 1) != 0))應該是

    while((nReadFile = read(nOpenFile, buffer, 1)) != 0) 
    

    在C中,操作者!=的優先級高於=,所以nReadFile = read(nOpenFile, buffer, 1) != 0 MEAS nReadFile = (read(nOpenFile, buffer, 1) != 0)

  4. if(nReadFile = write(1, buffer, nReadFile) == '\n')

    如果要檢查當期的字符是否是換行,你應該喜歡的東西if (buffer[0] == '\n')

  5. cout << nLineCount << endl;

    這是C++,不C.