Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 函数的作用是:获取位置_C_File_Position_Strstr - Fatal编程技术网

C 函数的作用是:获取位置

C 函数的作用是:获取位置,c,file,position,strstr,C,File,Position,Strstr,有两个文本,文本a是内容,文本b是逐行列出单词。该程序用于获取文本b中单词在内容中的位置 这是我的节目: #include<stdio.h> #include<string.h> #define WORDMAXLENGTH 30 #define MAXLENGTH 200 int main(){ typedef struct{ char stack[MAXLENGTH][WORDMAXLENGTH]; int top;

有两个文本,文本a是内容,文本b是逐行列出单词。该程序用于获取文本b中单词在内容中的位置

这是我的节目:

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

#define WORDMAXLENGTH 30
#define MAXLENGTH 200

int main(){
    typedef struct{
        char stack[MAXLENGTH][WORDMAXLENGTH];
        int top;
    }stack;

    stack query;
    query.top = 0;
    int i = 0, j = 0,q = 0;
    char myArr[MAXLENGTH];
    char *PosStr = NULL; 
    FILE *inFile = fopen("query.txt","r");
    FILE *inFile2 = fopen("hello.txt","r");

    while(fgets(query.stack[query.top],WORDMAXLENGTH,inFile) != NULL){
        query.top++;
    }

    fgets(myArr,MAXLENGTH,inFile2);

    for(i = 0; i < query.top; i++){
        PosStr = strstr(myArr,query.stack[i]);//get the position of s2 (Q1)
        printf("%d\n", PosStr -  myArr + 1);
    }

    fclose(inFile);
    fclose(inFile2);
    return 0;
}
#包括
#包括
#定义WORDMAXLENGTH 30
#定义MAXLENGTH 200
int main(){
类型定义结构{
字符堆栈[MAXLENGTH][WORDMAXLENGTH];
int top;
}堆叠;
堆栈查询;
query.top=0;
int i=0,j=0,q=0;
char myArr[MAXLENGTH];
char*PosStr=NULL;
文件*infle=fopen(“query.txt”、“r”);
文件*inFile2=fopen(“hello.txt”、“r”);
while(fgets(query.stack[query.top],WORDMAXLENGTH,infle)!=NULL){
top++;
}
fgets(myArr、MAXLENGTH、inFile2);
对于(i=0;i

问题1。这个等式对吗?如果是错的,我怎样才能得到这个职位?如果它是正确的,为什么我不能得到正确的位置?此外,PosStr的一些结果是0。

唯一的问题是
fgets()
'\n'
放入缓冲区,从而使
strstrstr()
也尝试匹配该字符,有多种方法可以删除该字符,一种简单的方法是

strtok(myArr, "\n");
就在
fgets()
之后,它工作,因为
strtok()
将用
'\0'
替换
'\n'
,或者

size_t length = strlen(myArr);
myArr[length - 1] = '\0';

我不完全确定我是否理解这个问题,但我猜“query.txt”(读入堆栈对象)由几行字组成(每行不超过30个字符),类似于 一些词 再多说几句 一行字 虽然“hello.txt”只包含一行,但您正在搜索的单词: 单词 您希望程序生成输出: 6. 11 1. 1. 对于上述输入

如注释中所述,fgets()函数将在其读取的缓冲区中包含终止“\n”。 另外,strstr()函数接受参数 char*strstr(const char*haystack,const char*needle); 也就是说,第一个参数是大字符串(草堆),您正在其中搜索小字符串(针)。它返回一个指向干草堆的指针,指示在哪里找到针。因此,如果我理解你的问题,该计划应该是:

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

#define WORDMAXLENGTH 30
#define MAXLENGTH 200
int
main()
{
    typedef struct {
        char stack[MAXLENGTH][WORDMAXLENGTH];
        int top;
    } stack;
    stack query;

    query.top = 0;
    int i = 0, j = 0, q = 0;

    char myArr[MAXLENGTH];
    char *PosStr = NULL;
    FILE *inFile = fopen("query.txt", "r");
    FILE *inFile2 = fopen("hello.txt", "r");

    while (fgets(query.stack[query.top], WORDMAXLENGTH, inFile) != NULL) {
        query.top++;
    }

    fgets(myArr, MAXLENGTH, inFile2);
        myArr[strlen(myArr)-1] = 0;

    for (i = 0; i < query.top; i++) {
        PosStr = strstr(query.stack[i], myArr); //get the position of s2 (Q1)

        printf("%d\n", PosStr -query.stack[i] + 1);
    }

    fclose(inFile);
    fclose(inFile2);
    return 0;
}
#包括
#包括
#定义WORDMAXLENGTH 30
#定义MAXLENGTH 200
int
main()
{
类型定义结构{
字符堆栈[MAXLENGTH][WORDMAXLENGTH];
int top;
}堆叠;
堆栈查询;
query.top=0;
int i=0,j=0,q=0;
char myArr[MAXLENGTH];
char*PosStr=NULL;
文件*infle=fopen(“query.txt”、“r”);
文件*inFile2=fopen(“hello.txt”、“r”);
while(fgets(query.stack[query.top],WORDMAXLENGTH,infle)!=NULL){
top++;
}
fgets(myArr、MAXLENGTH、inFile2);
myArr[strlen(myArr)-1]=0;
对于(i=0;i

特别是,你在一根针里寻找一个干草堆,而这根针实际上并不是你想要的

我假设该程序旨在检查第一个文件中的每个单词列表,检查第二个文件的单个文本行中是否出现了单词列表,只要稍加调整,它就可以工作

我添加了一些错误检查,并从文件输入中删除了尾随的
换行符。在基于
NULL
打印值之前,我检查了
strstr()
的结果。我还添加了另一个
#define
,以区分堆栈的大小和测试字符串的长度,并检查堆栈是否溢出

更新修改代码以检查整个单词-不区分大小写

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

#define WORDMAXLENGTH 30
#define MAXENTRY 200
#define MAXLENGTH 200

typedef struct{
    char stack[MAXENTRY][WORDMAXLENGTH];
    int top;
} stack;

int main(){
    FILE *inFile;
    FILE *inFile2;
    int i, w;
    char myArr[MAXLENGTH];
    char *sptr; 
    stack query;
    query.top = 0;
    inFile = fopen("query.txt","r");
    inFile2 = fopen("hello.txt","r");
    if (inFile == NULL || inFile2 == NULL) {
        printf("Cannot open both files\n");
        return 1;
    }
    while(fgets(query.stack[query.top], WORDMAXLENGTH, inFile) != NULL){
        i = strcspn(query.stack[query.top], "\r\n");
        query.stack[query.top][i] = 0;      // remove trailing newline etc
        if (++query.top >= MAXENTRY)        // check stack full
            break;
    }

    fgets(myArr,MAXLENGTH,inFile2);
    //myArr [ strcspn(myArr, "\r\n") ] = 0; // remove trailing newline etc
    w = 1;                                  // word count
    sptr = strtok(myArr, " \t\r\n");        // removes trailing stuff anyway
    while (sptr) {                          // each word in test string
        for(i=0; i<query.top; i++) {        // each word in library list
            if (stricmp(sptr, query.stack[i]) == 0)  // without case
                printf("%-4d %s\n", w, query.stack[i]);
        }
        w++;
        sptr = strtok(NULL, " \t\r\n");
    }

    fclose(inFile);
    fclose(inFile2);
    return 0;
}
文件
hello.txt

cat
dog
fox
rabbit
A quick brown fox jumps over the lazy dog
程序输出:

4    fox
9    dog
注意:以“//--”开头的注释是以下代码更改的原因
#包括
#包括//退出(),退出失败
#包括
//--换行#在括号中定义数字
//--垂直对齐使代码更易于阅读
//--垂直间距使代码更易于阅读
#定义WORDMAXLENGTH(30)
#定义最大长度(200)
//--将数据类型定义放在任何函数之外
//--在现代C中,对于结构定义,只需声明结构
//--并且不要用结构定义的typedef将代码弄乱
结构堆栈
{
字符堆栈[MAXLENGTH][WORDMAXLENGTH];
int top;
};
//--将大数据结构放在文件全局内存中,而不是堆栈上
//包含搜索键和搜索键数
静态结构堆栈查询;
//--对大括号使用格鲁吉亚格式使代码更难阅读
//--在大括号内缩进代码块以确保可读性
int main()
{
query.top=0;
//--虽然是合法的C,但在同一行上有多个变量声明
//--导致维护问题并降低可读性
int i=0;
//--消除未使用的变量
//int j=0;
//int q=0;
char myArr[MAXLENGTH];//要搜索的行
char*PosStr=NULL;//ptr到找到搜索键的位置
//--始终检查fopen返回的值,以确保操作成功
//--在比较时,始终将文字置于左侧
//--所以编译器可以捕获错误,比如使用“=”而不是“=”
文件*infle=fopen(“query.txt”、“r”);
if(NULL==infle)
{//然后fopen失败了
perror(“fopen for query.txt for read failed”);
退出(退出失败);
}
//否则,fopen成功了
文件*inFile2=fopen(“hello.txt”、“r”);
if(NULL==inFile2)
{//然后,fopen失败了
perror(“fopen for hello.txt for read failed”);
fclose(infle);//清除
退出(退出失败);
}
//否则,fopen将成功
Note: comments beginning with '// --' are reasons for following code changes

#include<stdio.h>
#include<stdlib.h> // exit(), EXIT_FAILURE
#include<string.h>

// --wrap #define number in parens
// --vertical alignment make the code easier to read
// --vertical spacing makes the code easier to read
#define WORDMAXLENGTH (30)
#define MAXLENGTH     (200)

// --place data type definitions outside of any function
// --in modern C, for struct definitions just declare the struct
// --and don't clutter the code with typedef's for struct definitions
struct stack
{
    char stack[MAXLENGTH][WORDMAXLENGTH];
    int top;
};

// --place large data struct in file global memory, not on stack
// contains search keys and number of search keys
static struct stack query;

// --using Georgian formatting for braces makes the code harder to read
// --indent code blocks within braces for readabillity
int main()
{
    query.top = 0;
    // --while legal C, multiple variable declarations on same line
    // --leads to maintenance problems and reduces readability
    int i = 0;
    // -- eliminate unused variables
    //int j = 0;
    //int q = 0;
    char myArr[MAXLENGTH]; // line to search
    char *PosStr = NULL;   // ptr to where search key found

    // --always check the returned value from fopen to assure operation successful
    // --always place the literal on the left in comparisons
    // --    so compiler can catch errors like using '=' rather than '=='
    FILE *inFile = fopen("query.txt","r");
    if( NULL == inFile )
    { // then fopen failed
        perror( "fopen for query.txt for read failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    FILE *inFile2 = fopen("hello.txt","r");
    if( NULL == inFile2 )
    { // then, fopen failed
        perror( "fopen for hello.txt for read failed" );
        fclose(inFile);  // cleanup
        exit( EXIT_FAILURE );
    }

    // implied else, fopen successful

    // --the following while loop can
    // -overflow the available space in the struct
    // --leading to undefined behaviour and can/will lead to a seg fault event
    // --comment the code so reverse engineering is not needed
    // note: each search key in the struct field: stack[]  will be terminated with '\n'
    //       so eliminate them
    // read in complete file. line-by-line to struct
    // while tracking number of lines
    while(fgets(query.stack[query.top],WORDMAXLENGTH,inFile))
    {
        query.top++;
        strtok(myArr, "\n"); // replace newline with NUL char   
    } // end while

    // --always check returned value from fgets
    // --to assure the operation was successful
    // read line to search
    if( NULL == fgets(myArr,MAXLENGTH,inFile2) )
    { // then fgets failed
        perror( "fgets for hello.txt file failed" );
        exit( EXIT_FAILURE );
    }

    // implied else, fgets successful

    for(i = 0; i < query.top; i++)
    {
        // --strstr will return NULL if search string not found
        // --always check returned value from strstr (!=NULL) to assure successful operation
        PosStr = strstr(myArr,query.stack[i]);//get the position of s2 (Q1)

        if( PosStr )
        { // then at least one instance of current search key found in line
            // --difference between two pointer is a 'long int', not an 'int'
            // display offset into line
            printf("%ld\n", PosStr -  myArr + 1);
        } // end if
    } // end for

    fclose(inFile);
    fclose(inFile2);
    return 0;
} // end function: main