Simple file input/output question

Asked 2 years ago, Updated 2 years ago, 50 views

Find the pattern in the string

Number of parts that match the pattern and string String index of the correct part

Write a code to print out the and on the current desktop You have saved three: reset.txt, string.txt, and pattern.txt. In the second file, for example, "Hello world my name is world", The third file has world as an example, so the file input is very good right now. In fact, the variable stack in the code has 2 in it, and if you print 6 or 23 degrees, which is the index of the part where the pattern fits, it is printed well, but strangely, I don't know why nothing is printed on the result.txt<

#include <stdio.h>
#include <string.h>
#define CMAX 10000000
#define PMAX 3000
int stack=0;
char space=' ';
char str[CMAX], pat[PMAX];
int stack_2[PMAX];
int nfind(char*, char*);
int main(){
    FILE *fp=fopen("string.txt", "rt");
    FILE *fp2=fopen("pattern.txt", "rt");
    fgets(str, CMAX, fp);
    fgets(pat, PMAX, fp2);
    for(int i=0;i<PMAX;i++)
        stack_2[i]=-1;
    nfind(str, pat);
    int S;
    S=stack;
    FILE *fp3;
    fp3=fopen("result.txt", "wt");
    if(fp3==NULL)
    {
        printf("failed\n");
        return 1;
    }
    fprintf(fp3, "%d\n", stack);
    while(stack>0)
    {
        fprintf(fp3, "%d ", stack_2[stack-1]);
        fprintf(fp3, "%c", space);
        stack--;
    }
    fclose(fp);
    fclose(fp2);
    fclose(fp3);
    return 0;
}
int m=0;
int nfind(char *string, char *pat)
{
    int i, j, start=0;
    int n;
    int lasts=strlen(string)-1;
    int lastp=strlen(pat)-1;
    int endmatch=lastp;
    for(i=0;endmatch<=lasts;endmatch++, start++){
        if(string[endmatch]==pat[lastp])
        {
            for(j=0, i=start;j<=lastp&&string[i]==pat[j];i++)
            {
                j++;

            }


        }
        if(j==lastp+1)
       {

             stack_2[m]=start;
             m++;
             stack++;
             j=0;
        }
        else
            continue;

    }
    return 0;

}

file io

2022-09-20 17:21

1 Answers

If you create and run result.txt and nothing is written, there is a very high probability... The problem lies not in the code, but in the permissions and file processing section.

By default, the program that creates the file is:

In other words, this code should be rotated without result.txt. Delete result.txt and try again. If you get an error about permission, please check if the Java application you are running has write permission in the path of the problem.


2022-09-20 17:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.