1

I want to achieve the following: I have to manage a database in a file( for example txt) which have the following structure: X,X,X,X,X In this database I want to be able to modify a line, which works perfectly when I write the line to be changed in the code. However I want to be able to write the line during running the program. Important: when you write in a line value, you have to put a . to the end but that . will not make it to the file. Also, you don't know how long the input is. this works: In Main the values are pre-given:

char file[50];
scanf("%s",file);
char tmp[]="temp.txt";
char a[]="2,2,2,2,2";
char b[]="9,9,9,9,9";
javitas(file,tmp,a,b);  // this will modify the line a to b in the file

The procedure:

#define sorhossz 500
void javitas(char filename1[], char filename2[], const char *str, const char *replace) {
    FILE *in = fopen(filename1,"r+");
    FILE *out = fopen(filename2,"w+");
    size_t strl;
    char line[sorhossz];
    strl = strlen(str);
    while (fgets(line, sizeof(line), in)) {
        char *y, *x = line;
        while ((y = strstr(x, str))) {
            fwrite(x, 1, y - x, out);
            fputs(replace, out);
            x = y + strl;
        }
        fputs(x, out);

    }

    rewind(out);
    rewind(in);
    while (fgets(line, sizeof(line), out))
    {
        char *x = line;
        fputs(x, in);
    }
    fclose(in);
    fclose(out);
}

but I want to give 'a' and 'b' during the running somehow like this in Main:

char a[50]; // 50 is a random number I was triing with
char b[50];
printf("Give the line you want to modify\n");
scanf("%[^.]%*c",a);
printf("Give the line you want to modify to\n");
scanf("%[^.]%*c",b);
javitas(file,tmp,a,b);

when i try to give the values from the input, i write "2,2,2,2,2." for a and "9,9,9,9,9." for b. Dont forget that the . is not making into the string. After that, I can printf these strings inside the procedure so I guess the procedure understand the input value, however it does not modify the line 'a' to line 'b' in the file. Could you please help?

and this is a file im testing on: (I named it qqq.txt but it doesnt matter)

9,9,9,9,9
hel,az,de,5,k
0
ennyi,annyi,amannyi,helo,ot 22

1,2,3,4,5

1,2,2,2,3

ez,az vegre, sikerult, 3 sort illeszteni, a fileba

peti,egy,kiraly,helo,5om, 5 helo 5, 5 ezaz
egy,ketto,harom,negy,ot
helo,helo,lusta,vagyok,irni
meg,egyszer,hatha,most,sikerul
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,1,1,1,1
2,2,2,3,3
helo,belo,ize,de,4

If the text file doesnt exits, it will be created before the menu. Sorry for being hungarian here and there, but I hope the main thing is understandable.

8
  • 3
    "but this is not working at all" That leaves quite some room for guessing what that might mean. Please be specific about what is not working. What do you provide as input, what do you expect, what do you get instead? Commented Nov 1, 2021 at 10:09
  • Your question asks about passing strings to a function but did you verify that your input strings already look OK before you pass them to your function? Commented Nov 1, 2021 at 10:14
  • @Gerhardh when i try to give the values from the input, i write "2,2,2,2,2." for a and "9,9,9,9,9." for b. Dont forget that the . is not making into the string. After that, I can printf these strings inside the procedure so I guess the procedure understand the input value, however it does not modify the line 'a' to line 'b' in the file. Commented Nov 1, 2021 at 11:06
  • Please add this vital information to the question, not in comments. Commented Nov 1, 2021 at 11:11
  • 2
    If I get you correctly, if works if you define the strings as literals in main but not if you use strings from user input. That means it is extremely unlikely that something is going wrong when passing the values to your function. Something with your input is wrong. Do you press enter after a and b or do you add both in same line? In the first case I expect you have a leading \n in b. Commented Nov 1, 2021 at 11:22

1 Answer 1

1

when i try to give the values from the input, i write "2,2,2,2,2." for a

Review scanf("%[^.]%*c",a);

This and the prior scanf() calls do not consume the input lines' trailing '\n', so in this case a becomes "\n2,2,2,2,2".

A simplistic fix is scanf(" %[^.]%*c",a); (note the space) to consume leading white-spaces like '\n'.

Better code would use a width limit, scan for a '.' and check result and maybe the '\n'

char a[50];
if (scanf(" %49[^.].%*1[\n]",a) == 1) Success(); else Fail();

More robust code would not use scanf() at all but read the user input line with fgets() and then process the string.


Other issues may exist.

Sign up to request clarification or add additional context in comments.

2 Comments

the space in the scanf did the trick! thank you very much!
@Frank Just in case: You should not just take the space but also the length indicator. That will not only solve your current headache but also possible future headache.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.