簡體   English   中英

C…字符串分割問題

[英]C…string splitting issue

我的firstCheck()函數遇到問題。 我將在下面用當前代碼直接進行解釋。 我已經使用我所有的C ++知識編寫了該程序。

firstCheck()函數無法正常工作。 在readFile()函數中,我已成功地將給定文件中的文本逐行拆分為一個數組。 然后firstCheck()應該使用該數組“ myString”,並讀取第一個字符串,直到出現“”(基本上是第一個單詞/字符/等),以便我對其進行評估。

我很確定我的問題是這部分。 該程序似乎停止並等待由於“ stdin”而導致的輸入,那么實現此代碼段的更好方法是什么?

while(strcmp(fgets(item.myString,sizeof item.myString,stdin),“”)!= 0)
{myWord [s] = strdup(item.myLines [s]); }

我應該改用scanf()嗎? 有人告訴我使用gets()是不好的做法,所以我改用fgets()

大會

#include "assem.h"

int readFile(FILE *file, struct assem *item) 
{
    size_t i =0;
    item->counter = 0;  //this is sort of the constructor, if you will. 
    size_t maxLines = sizeof item->myLines / sizeof *item->myLines;  //breaks down text file into line by line 
    while(i < maxLines && fgets(item->myString, sizeof item->myString, file))  //and stores them into "myLines array"
    {
        item->myLines[i] = strdup(item->myString);
        i++;
        item->counter++;
    }   

    return 0;
}

void printFile(struct assem item)  //just printing things..prints it with a new line in front
{
    printf("\n");
    for(int s = 0; s < item.counter; s++)
    {
        printf("%s\n", item.myLines[s]);
    }
    printf("\n");
}

int firstCheck(struct assem item)               
{
    char *myWord [7] = {NULL};

    for(int s = 0; s < item.counter; s++)   
    {
        while(strcmp( fgets( item.myString, sizeof item.myString, stdin ), " " ) != 0 )         
        {
            myWord[s] = strdup(item.myLines[s]);
        }           
    }

    for(int s = 0; s < item.counter; s++)
    {
        printf("%s\n", myWord[s]);
    }

    return 0;       
}

“ assem.h”

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

struct assem
{
    char myString[101];  //buffer
    char *myLines[20];  //this will store the lines from a text file..up to 20 lines
    int counter;  //counter for number of lines
    //printing stuff...prints file directly from whats STORED IN THE ARRAY
};

int readFile(FILE *FileToBeRead, struct assem *item);  //takes in the file that needs to be read and splits it into lines
int firstCheck(struct assem item);
void printFile(struct assem item);

“ main.c”

#include "Assem.c"

int main()
{
    struct assem test;
    FILE *mips;
    mips = fopen("/home/rpf0024/cse2610/Program1/mips.asm", "r");
    readFile(mips, &test);
    printFile(test);
    firstCheck(test);
    fclose(mips);
    return 0;
}

回答“實現此代碼段的更好方法是什么?” 而不看其他代碼:

while(strcmp( fgets( item.myString, sizeof item.myString, stdin ), " " ) != 0 )

這將導致strcmp()在文件的結尾段錯誤 ,因為fget()將返回NULL

另請注意, fgets()將在輸入末尾保留所有newline 因此,將輸入與" "進行比較將永遠不會匹配(除非文件的最后一行沒有newline )。

這樣比較好

while (NULL != fgets(item.myString, sizeof item.myString, stdin)) {
    if (strcmp(item.myString, " \n"  != 0) {
       ...
    }
}

但是我不確定這對您是否也有用,因為您可能想在字符串而不是整個文件行中查找space 在這種情況下,您應該研究strtok()

char *tok;
while (NULL != fgets(item.myString, sizeof item.myString, stdin)) {

    tok = strtok(item.myString, " \t\r\n");
    while (tok) {
        printf("%s\n", tok);
        tok = strtok(NULL, " \t\r\n");
    }

}

這會將您的字符串分成由空格或其他空格分隔的部分。 但是請注意,通過item.myString化, item.myString將在該過程中被分段。

暫無
暫無

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

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