-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refined some of the bugs of gets() and freed the memory
- Loading branch information
1 parent
6b03529
commit 74c2edd
Showing
29 changed files
with
485 additions
and
428 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void calc() | ||
{ | ||
char type; | ||
printf("Enter d for decimal operations and i for integer operation: "); | ||
scanf("%c", &type); | ||
if (type == 'i') | ||
{ | ||
getchar(); | ||
int a, b; | ||
printf("Enter first number: "); | ||
scanf("%d", &a); | ||
printf("Enter second number: "); | ||
scanf("%d", &b); | ||
char o; | ||
printf("Enter operation(+, -, /, *): "); | ||
getchar(); | ||
scanf("%c", &o); | ||
if (o == '+') | ||
{ | ||
printf("%d\n\n", a + b); | ||
} | ||
else if (o == '-') | ||
{ | ||
printf("%d\n\n", a - b); | ||
} | ||
else if (o == '*') | ||
{ | ||
printf("%d\n\n", a * b); | ||
} | ||
else if (o == '/') | ||
{ | ||
printf("%d\n\n", a / b); | ||
} | ||
} | ||
else | ||
{ | ||
getchar(); | ||
float x, y; | ||
printf("Enter first number: "); | ||
scanf("%f", &x); | ||
printf("Enter second number: "); | ||
scanf("%f", &y); | ||
char o; | ||
printf("Enter operation(+, -, /, *): "); | ||
getchar(); | ||
scanf("%c", &o); | ||
if (o == '+') | ||
{ | ||
printf("%.2f\n\n", x + y); | ||
} | ||
else if (o == '-') | ||
{ | ||
printf("%.2f\n\n", x - y); | ||
} | ||
else if (o == '*') | ||
{ | ||
printf("%.2f\n\n", x * y); | ||
} | ||
else if (o == '/') | ||
{ | ||
printf("%.2f\n\n", x / y); | ||
} | ||
} | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
void calc() | ||
{ | ||
char type; | ||
printf("Enter d for decimal operations and i for integer operation: "); | ||
scanf("%c", &type); | ||
if (type == 'i') | ||
{ | ||
getchar(); | ||
int a, b; | ||
printf("Enter first number: "); | ||
scanf("%d", &a); | ||
printf("Enter second number: "); | ||
scanf("%d", &b); | ||
char o; | ||
printf("Enter operation(+, -, /, *): "); | ||
getchar(); | ||
scanf("%c", &o); | ||
if (o == '+') | ||
{ | ||
printf("%d\n\n", a + b); | ||
} | ||
else if (o == '-') | ||
{ | ||
printf("%d\n\n", a - b); | ||
} | ||
else if (o == '*') | ||
{ | ||
printf("%d\n\n", a * b); | ||
} | ||
else if (o == '/') | ||
{ | ||
printf("%d\n\n", a / b); | ||
} | ||
} | ||
else | ||
{ | ||
getchar(); | ||
float x, y; | ||
printf("Enter first number: "); | ||
scanf("%f", &x); | ||
printf("Enter second number: "); | ||
scanf("%f", &y); | ||
char o; | ||
printf("Enter operation(+, -, /, *): "); | ||
getchar(); | ||
scanf("%c", &o); | ||
if (o == '+') | ||
{ | ||
printf("%.2f\n\n", x + y); | ||
} | ||
else if (o == '-') | ||
{ | ||
printf("%.2f\n\n", x - y); | ||
} | ||
else if (o == '*') | ||
{ | ||
printf("%.2f\n\n", x * y); | ||
} | ||
else if (o == '/') | ||
{ | ||
printf("%.2f\n\n", x / y); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include "change_dir_path.c" //including the change_dir_path.h to get the variable change_dir. | ||
#include <dirent.h> | ||
#include <unistd.h> | ||
|
||
void ccwd() | ||
{ | ||
printf("Enter the path to change this directory to: \n"); | ||
getchar(); | ||
fgets(change_dir, 100, stdin); | ||
change_dir[strlen(change_dir) - 1] = 0; | ||
if (chdir(change_dir) == 0) //implementing chdir function. | ||
{ | ||
printf("successfully changed\n"); | ||
} | ||
else | ||
{ | ||
printf("Cannot change the directory.\n"); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
char change_dir[100];//assigning at least 100 characters to the new directory name entered by user. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,28 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
|
||
//function to change the username and password | ||
void change(){ | ||
char username_new[10], password_new[10]; | ||
int i =0; | ||
getchar(); | ||
printf("Now you can change the username and password.\n"); | ||
printf("Enter new username-"); | ||
gets(username_new);//new username | ||
printf("Enter new password-"); | ||
gets(password_new);//new password | ||
FILE *new_user; | ||
FILE *new_password; | ||
//writing in the files the new username and password | ||
new_user = fopen("root_username.txt", "w"); | ||
new_password = fopen("password_root.txt", "w"); | ||
fprintf(new_user, username_new); | ||
fprintf(new_password, password_new); | ||
fclose(new_user); | ||
fclose(new_password); | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
|
||
//function to change the username and password | ||
void change(){ | ||
char username_new[10], password_new[10]; | ||
int i =0; | ||
getchar(); | ||
printf("Now you can change the username and password.\n"); | ||
printf("Enter new username-"); | ||
fgets(username_new, 10, stdin);//new username | ||
username_new[strlen(username_new) - 1] = 0; | ||
|
||
printf("Enter new password-"); | ||
fgets(password_new, 10, stdin);//new password | ||
password_new[strlen(password_new) - 1] = 0; | ||
|
||
FILE *new_user; | ||
FILE *new_password; | ||
//writing in the files the new username and password | ||
new_user = fopen("root_username.txt", "w"); | ||
new_password = fopen("password_root.txt", "w"); | ||
fprintf(new_user, username_new); | ||
fprintf(new_password, password_new); | ||
fclose(new_user); | ||
fclose(new_password); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//I have made this function thinking that the clearScreen will work on every system. | ||
void clearScreen() | ||
{ | ||
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__) | ||
system("clear"); | ||
#endif | ||
|
||
#if defined(_WIN32) || defined(_WIN64) | ||
system("cls"); | ||
#endif | ||
} | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
//I have made this function thinking that the clearScreen will work on every system. | ||
void clearScreen() | ||
{ | ||
#if defined(__linux__) || defined(__unix__) || defined(__APPLE__) | ||
system("clear"); | ||
#endif | ||
|
||
#if defined(_WIN32) || defined(_WIN64) | ||
system("cls"); | ||
#endif | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
// Fucntion to copy file | ||
void copy() | ||
{ | ||
FILE *ptr1; | ||
FILE *ptr2; | ||
char filename1[25], filename2[25], content; | ||
|
||
getchar(); | ||
printf("Enter the filename to be copied- "); | ||
fgets(filename1, 25, stdin); | ||
filename1[strlen(filename1) - 1] = 0; | ||
|
||
printf("Now enter the name of the file in which the contents of the %s to be copied - ", filename1); | ||
fgets(filename2, 25, stdin); | ||
filename2[strlen(filename2) - 1] = 0; | ||
|
||
ptr1 = fopen(filename1, "r"); | ||
|
||
if (ptr1 == NULL) | ||
{ | ||
printf("Cannot copy\n"); | ||
} | ||
else | ||
{ | ||
ptr2 = fopen(filename2, "w"); | ||
|
||
content = fgetc(ptr1); | ||
|
||
while (content != EOF) | ||
{ | ||
fputc(content, ptr2); | ||
content = fgetc(ptr1); | ||
} | ||
|
||
printf("\nSuccessfully copied to %s \n", filename1); | ||
fclose(ptr2); | ||
} | ||
|
||
fclose(ptr1); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,12 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
|
||
//date function. If the user clears the screen it can be usefule to see the date. | ||
void date() | ||
{ | ||
time_t t; | ||
time(&t); | ||
printf("%s\n\n", ctime(&t)); | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <time.h> | ||
|
||
|
||
//date function. If the user clears the screen it can be usefule to see the date. | ||
void date() | ||
{ | ||
time_t t; | ||
time(&t); | ||
printf("%s\n\n", ctime(&t)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.