Home > Net >  Passing input string to a function in C
Passing input string to a function in C

Time:11-02

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.

CodePudding user response:

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(" I[^.].%*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.

  • Related