identify function signature - c

I am writing a program in c to identify functions' signature and copy in another file.
Idea was to determine parenthesis in any line and to copy that line to a file.
Afterwards we can check for return type and parameters, so as to differentiate constructs if, while from user defined functions except main.
But my code stuck in infinite loop. Can't find the problem.
find_sig.c
#include <stdio.h>
int main()
{
int count=0;
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
{
do
{
if(c=='(')
{
fseek(f1,-count,1);
do{
putc(c,f2);
}while((c=getc(f1))!=')');
count=0;
}
else
count++;
}while((c=getc(f1))!=10);
count=0;
}
fclose(f1);
fclose(f2);
return 0;
}
input.c
#include<stdio.h>
void fun();
int main()
{
fun();
return 0;
}
void fun()
{
printf("hello");
}
Any other idea for determining functions' signature will be very helpful.

i figured it out.
#include<stdio.h>
#include<string.h>
char str1[50];
int count=0,i=0;
int main()
{
char c;
FILE *f1,*f2;
f1=fopen("input.c","r");
f2=fopen("output.c","w");
while((c=getc(f1))!=EOF)
{
if(c!=10) //checks for /n
{
if(c=='(')
{
++count;
fseek(f1,-count,1); //moves f1 to 'count' bytes back i.e. beginning of line
i=0;
while((c=getc(f1))!=';'&&c!='{') //checks for declaration or definition
{
str1[i++]=c;
}
if(strstr(str1,"main(")!=NULL) //checks whether str1 contains main
return 0;
else
{
fprintf(f2,"%s",str1); // copies str1 in f2
count=0;
}
}
else
count++;
}
else
count=0;
if(c==10)
putc(c,f2);
}
fclose(f1);
fclose(f2);
return 0;
}

Related

returning permutes in a string in C

I need to write a function that returns every permute of a word in a given text file.
For some reason the output is wrong and I don't really understand why.
However, if instead of writing a function that should check a presnce of a letter (as seen below the function chars(char,char*))
I write the needed letters for check manually
it works as intended.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100
void permute(FILE *fp, char *perm){
int c,i,perm_size=0;
int flag,chars(char,char *);
char permute[MAX];
i=0;
while(perm[i++])
perm_size++;
flag=0;
i=0;
while(!feof(fp)){
c=fgetc(fp);
if(chars(c,perm)==0){/*a function that checks c with each one of the permutes chars*/
flag++;
permute[i++]=c;
if (flag == perm_size){/*if the permute is the word's length*/
permute[i]='\0';
printf("%s\n",permute);
flag=0;
i=0;
permute[0]='\0';
}
}
else{
flag=0;
i=0;
}
}
}
int chars(char ch, char *str){
while(*str++)
{
if(ch==*str)
return 0;
}
return 1;
}
#include "func.h"
#include <string.h>
int main(int argc,char **argv){
FILE *fp;
char *input,*perm;
char *prog=argv[0];
void permute(FILE *, char *);
if(argc==1)
{
fprintf(stderr,"%s error: no arguments\n",prog);
exit(1);
}
input=argv[1];
perm=argv[2];
if(!(fp=fopen(input,"r")))
{
fprintf(stderr,"%s error: cannot open file\n",prog);
exit(1);
}
permute(fp,perm);
fclose(fp);
return 0;
}
input:
./program text chairs
output:
nothing
As told - you should really learn debuging your programs, so I did - I incremented wrongly a value in a function chars(char , char *)
a good pointer explanation from other user

Authentication in C (read and check from given file)

I have created the code below. What i need to do is that I need to get and input from the user(username and password) and I need to check with a file what I entered. The Problem forces me not to copy the whole .txt file in once. The .txt file is converted like this:
root
jfl34jgf
ben
12b43
stella
ldfo421
kate
jfd45g
bill
iu3556
guest
1234
test
1234
So I was thinking of getting all the user names, putting them into a list and then checking each one of them with the input. I created a struct that includes the username name, the position at which username name ends (using ftell()) and the next pointer that points to the next element. As you can see the text is formatted in the way that one like is username and the other is the password. so the password comes right after the username. using ftell() for each username if the comparison of name gives me true than I can check for the password check.
This is my code until now.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct names
{
char name[20];
int pos;
struct names *next;
};
typedef struct names Names;
void create_list(Names *l, char *temp1, int pos)
{
Names *temp;
temp=(Names*)malloc(1*sizeof(Names));
temp->next=NULL;
strcpy(temp->name, temp1);
temp->pos=pos;
temp->next=l;
printf("%s ", temp->name);
l=temp;
printf("%s\n", l->name);
}
void create_list_from_File(FILE *fp, Names *l)
{
char ch;
int i=0, check=0, pos;
char temp1[20];
temp1[0]='\0';
while(1)
{
if(check==1)
break;
while((ch=fgetc(fp))!='\n')
{
temp1[i]=ch;
i++;
}
temp1[i]='\0';
pos=ftell(fp)+1;
create_list(l,temp1, pos);
while((ch=fgetc(fp))!='\n')
{
if(ch==EOF)
{
printf("EOF ");
check=1;
break;
}
}
i=0;
}
}
void display(Names *l)
{
Names *cursor;
cursor=l;
while (cursor!=NULL)
{
printf("%s\n", cursor->name);
cursor=cursor->next;
}
}
int main()
{
char usern[20][20];
char passw[20];
FILE *fp;
Names l;
l.next=NULL;
char c;
int i=0;
fp=fopen("users.txt", "r");
if(fp==NULL)
{
printf("File not opened!\n");
exit(1);
}
create_list_from_File(fp, &l);
display(&l);
/* fgets(usern, 20, stdin);
usern[strlen(usern)-1]='\0';
while(strcmp(usern, "exit")!=0)
{
fgets(passw,20, stdin);
passw[strlen(passw)-1]='\0';
check(fp, usern, passw);
}
*/
return 0;
}
Right now the I do not see stuff inside the linked list. I am getting the strings from the file correctly ( the username are printed) but when I try to print the list it just gives me some weird values. Help would be much appreciated.
The provided source code presents a classical error when managing linked-list. Modifying the content of a pointer inside a function when passed as value as only a local effect and doesn't affect the value in the calling function.
Problem - Modifying the value of Names *l in the function create_list().
The function create_list() adds a new node Names *temp; in front of the list to increase performance by modifying only the first node.
The new node is pointing to the first temp->next=l;,
Then the new node becomes the first l=temp;.
When printing locally values, seems no problem.
But, the assignment l=temp; is only available inside the function (assignment to a local Names *l pointer) and when return to the calling function create_list_from_File() the l doesn't change.
Solution 1 - Modify the value of the l->next is the fastest solution.
In the main(), a starting Names l; has been declared with an assignment l.next=NULL; meaning (list empty).
In the create_list() function, the assignment is:
strcpy(temp->name, temp1);
temp->pos=pos;
// assigning to the next pointer
temp->next=l->next;
printf("%s ", temp->name);
// modify the next pointer
l->next=temp;
printf("%s\n", l->next->name);
Instead of:
strcpy(temp->name, temp1);
temp->pos=pos;
temp->next=l;
printf("%s ", temp->name);
l=temp;
printf("%s\n", l->name);
To be complete, for that shortest solution, the function display() shall start from the l->next which is the first node.
void display(Names *l)
{
Names *cursor;
cursor=l->next;
while (cursor!=NULL)
{
printf("%s\n", cursor->name);
cursor=cursor->next;
}
}
Thank you #J.Piquard for the help given above. I managed to solve this problem. I am posting the code below and the code can be tested in the above .txt file. Firstly an user enters the input and after that the username followed by a password. It is assumed that the username input will be always correct, but to check username is not that hard.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct names
{
char name[20];
int pos;
struct names *next;
};
typedef struct names Names;
Names *create_list(Names *l, char *temp1, int pos)
{
Names *temp;
temp=(Names*)malloc(1*sizeof(Names));
strcpy(temp->name, temp1);
temp->pos=pos;
temp->next=l;
return temp;
}
Names * create_list_from_File(FILE *fp)
{
char ch;
int i=0, check=0, pos;
Names *l=NULL;
char temp1[20];
temp1[0]='\0';
while(1)
{
if(check==1)
break;
while((ch=fgetc(fp))!='\n')
{
temp1[i]=ch;
i++;
}
temp1[i]='\0';
pos=ftell(fp)+1;
l=create_list(l,temp1, pos);
while((ch=fgetc(fp))!='\n')
{
if(ch==EOF)
{
check=1;
break;
}
}
i=0;
}
return l;
}
int check_usrn(char *s, Names *l, int *i)
{
int flag=0;
Names *cursor=l;
*i=0;
while (cursor!=NULL)
{
if(strcmp(cursor->name, s)==0)
{
flag=1;
break;
}
cursor=cursor->next;
(*i)++;
}
return flag;
}
int check_passw(FILE *fp, Names *l, char *passw, int *i)
{
Names * cursor;
cursor=l;
int m=0, flag=0;
char c, temp[20];
while(m!=*i)
{
cursor=cursor->next;
m++;
}
m=0;
fseek(fp, cursor->pos-1, SEEK_SET);
while((c=fgetc(fp))!='\n')
{
if(c==EOF)
break;
temp[m]=c;
m++;
}
temp[m]='\0';
if(strcmp(passw, temp)==0)
flag=1;
return flag;
}
int main()
{
char usern[20];
char passw[20];
char file_name[20];
FILE *fp;
Names *l=NULL;
int i=0;
fgets(file_name, 20, stdin);
file_name[strlen(file_name)-1]='\0';
fp=fopen(file_name, "r");
if(fp==NULL)
{
printf("File not opened!\n");
exit(1);
}
l=create_list_from_File(fp);
while(strcmp(usern, "exit")!=0)
{
fgets(usern, 20, stdin);
usern[strlen(usern)-1]='\0';
if(check_usrn(usern, l, &i)==1)
{
fgets(passw, 20, stdin);
passw[strlen(passw)-1]='\0';
if(check_passw(fp ,l, passw, &i)==1)
printf("Access to user %s is granted.\n", usern);
else
printf("Access to user %s is denied.\n", usern);
}
}
printf("Exiting ...\n");
fclose(fp);
return 0;
}

Program not giving any output after taking input from user

I was creating on a very basic program in C which takes a word from user as input and searches for how many times it appears in a text file and gives output.
The code is:
#include<stdio.h>
#include<string.h>
int main()
{
char user[20];
char word[20];
int i,pos=0,sum=0;
char c;
c='a';
printf("Enter the word you want to look for\n");
gets(user);
FILE *p;
p=fopen("D:\\trees.txt","r+");
do
{
pos=0;
fscanf(p,"%s",word);
if(c!=EOF)
{
if(strlen(word)==strlen(user))
{
for(i=0;i<strlen(user);i++)
{
if(word[i]==user[i]||word[i]==user[i]+32||word[i]==user[i]-32)
{
}
else
{
pos=1;
break;
}
}
}
else
{
pos=1;
}
if(pos=0)
{
sum++;
}
}
}
while(c!=EOF)
;printf("\nNumber of times %s appears is %d",user,sum);
fclose(p);
}
Now the program takes the input fine, but doesn't give any output.
Looks like this:
What have I done wrong?
Looking at the comments, your code should be something like:
#include<stdio.h>
#include<string.h>
#include <ctype.h>
int main()
{
char user[20];
char word[20];
int n, pos=0, sum=0;
unsigned int i, l;
FILE *p;
do {
printf("Enter the word you want to look for\n");
} while (gets(user)==0);
user[strlen(user)-1]= '\0'; // remove trailing \n
if ((p=fopen("D:\\trees.txt","r+"))==0) {printf("Error opening file\n"); exit(0);}
do
{
pos=0;
n= fscanf(p,"%s",word);
if (n==1)
{
if(strlen(word)==(l=strlen(user)))
{
for(i=0; i<l; i++)
{
if(!(word[i]==user[i]||word[i]==tolower(user[i])||word[i]==toupper(user[i])))
{
pos=1;
break;
}
}
}
else pos=1;
if(pos==0) sum++;
}
}
while(n==1);
printf("\nNumber of times %s appears is %d",user,sum);
fclose(p);
return(1);
}
(with some optimizations and additions)

Only last fprintf() output is written to the file

I'm trying to write a program which encrypts a input file and creates an output file after the encryption.
The fprintf does work fine if I write to stdout but when I send the output to a file, only the last fprintf output is written into the file, i.e. if the input file contains 2 lines, the output file contains only the last line encrypted.
Source:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define Max 1024
int menu()
{
printf("To encrypt, input e or E\n");
printf("To decrypt, input d or D\n");
printf("To exit, input any other letter\n");
printf("Please enter your choice and hit return:\n");
return 0;
}
void encrypto(char*str)
{
FILE *ausgabeverschl;
ausgabeverschl=fopen("encrypted.txt","w");
int n=0;
char *p=str ,q[Max];
while(*p)
{
if(islower(*p))
{
if((*p>='a')&&(*p<'x'))
q[n]=toupper(*p + (char)3);
}
else
{
q[n]=*p+(char)3;
}
n++; p++;
}
q[n++]='\0';
fprintf(ausgabeverschl, "%s\n", q); // problem occurs here
fclose(ausgabeverschl);
}
void decrypto(char*str)
{
FILE *ausgabeunverschl;
ausgabeunverschl=fopen("decrypted.txt","w");
int n=0;
char *p=str, q[Max];
while(*p)
{
if(isupper(*p))
{
if((*p>='D')&&(*p<='Z'))
q[n]=tolower(*p - (char)3);
}
else
{
q[n]=*p-(char)3;
}
n++; p++;
}
q[n++]='\0';
fprintf(ausgabeunverschl,"%s\n",q);
fclose(ausgabeunverschl);
}
int main()
{
char einlesestring[Max];
char choice[2];
FILE *ein;
ein=fopen("text.txt","r");
if(ein!=NULL)
{
printf("\nFile found.\n\n");
}
if(ein==NULL)
{
printf("No file found.\n");
return 1;
}
int counter=0;
char stringlaenge[Max];
while (fgets(stringlaenge, Max, ein) != NULL)
{
counter++;
}
printf(„Counted lines: %d\n", counter);
rewind(ein);
menu();
gets(choice);
if((choice[0]=='e')||(choice[0]=='E'))
{
while(fgets(einlesestring, Max, ein)!= NULL)
{
encrypto(einlesestring);
}
}
else if((choice[0]=='d')||(choice[0]=='D'))
{
while(fgets(einlesestring,Max, ein)!=NULL)
{
decrypto(einlesestring);
}
}
fclose(ein);
return 0;}
Thanks for helping!

Trying to do dynamic memory allocation in words (text) from file in c

im trying to find out the number of different words of a text in a file, using dynamic memory allocation. however, i dont get the right results. the text can contain punctuation. the program is below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int different_words(FILE *fp);
int main(int argc, char *argv[]) {
FILE *fp;
different_words(fp);
return 0;
}
int different_words(FILE *fp) {
int i,j,ic=0,sum=0,sum2=0;
char d[100];
char **A;
fp=fopen("file.txt","rt");
if ((fp = fopen("file.txt", "rt"))== NULL) { //opening the file
printf("cannot read file\n");
exit(1);
}
while (fscanf(fp,"%s",&d)!=EOF)
sum++;
A=(char**)malloc(sum*sizeof(char*)); //allocate memory for all the words
if (A==NULL) {
fclose(fp);
return 0;
}
rewind(fp);
while(fscanf(fp,"%s",&d)!=EOF){
if (strchr("!?.,:",d[strlen(d)-1])==0) //edit
A[ic]=(char*)malloc(strlen(d)+1);
else
A[ic]=(char*)malloc(strlen(d));
if (A[ic]==NULL) {
fclose(fp);
return 0;
}
if (strchr("!?.,:",d[strlen(d)-1])!=0)
for (j=0;j<=strlen(d)-2;j++)
A[ic][j]=d[j];
else
strcpy(A[ic],d);
if (++ic==sum)
break;
}
for (i=0;i<sum;i++){
for (j=0;j<i;j++){
if (strcmp(A[i],A[j])==0)
break;
}
if (i==j) {
sum2++; //finding the number of different words in the text
}
}
printf ("Number of different words in the text: %d\n",sum2);
return sum2;
}
----------
The problem is here:
if (A=NULL) {
fclose(fp);
return 0;
}
You're assigning A to NULL instead of checking it for NULL. Change it to
if (A==NULL) {
fclose(fp);
return 0;
}
Also, the consensus is you shouldn't cast the return value of malloc and you should also take a look at this regarding reading data from files.

Resources