So I am writing a CS code for a project and I keep getting \"cannot open file\" I know I put it into
my code, but I don\'t know why it won\'t execute after i input ./a.out I put in gcc -Wall
contents3.c and no errors or warnings pop up. So I figure everything is working right. But after
that, I get \"Cannot open file\". So I am wondering what I can do to get this settled out properly. I
am putting the outcome of the project and my code. Please someone help me!
#include
#include
#include
#include
#include
#include
int len = 0;
int sp = 0;
int lwrcaseCount = 0;
int uprcaseCount = 0;
int digitCount = 0;
int specialCount = 0;
char data[200];
int frequency[84];
int sortFrequency[84][2];
char string[] =
\"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\\\"#$%&\'()*+,-
./:;<=>?@[/]^_`{|}~\";
//printf(\"\ \");
//To sort based on frequency of characters
/*void sort()
{
int max, loc, temp, x, y;
//Loops till end - 1 position of the frequency array
for(x = 0; x <= 84 - 1; x++)
{
//Initializes the max to the x position of the frequency array assuming it as maximum
max = frequency[x];
//Initializes of loc to x assuming location x contains the maximum value
loc = x;
//Loops through x + 1 to length - 1 of frequency array
for(y = x + 1; y <= 95 -1; y++)
{
//If current position of the frequency of the array is greater than the max
//then update the max with the current value of frequency and update the loc
if(frequency[y] > max)
{
max = frequency[y];
loc = y;
}//end of if
}//end of for loop
//If loc is not x then swap
if(loc != x)
{
temp = frequency[x];
frequency[x] = frequency[loc];
frequency[loc] = temp;
}//end of if
//Store the location of the maximum value in the zero column position of row x
sortFrequency[x][0]= loc;
//Store the maximum value in the first column position of row x
sortFrequency[x][1] = max;
}//end of for loop
}//end of function*/
void sort()
{
for(int i = 0; i < 84; i++)
{
sortFrequency[i][0] = i;
sortFrequency[i][1] = frequency[i];
}
for(int i = 0; i < 84-1; i++)
for(int j = 0; j < 84-i-1; j++)
if(sortFrequency[j][1] < sortFrequency[j+1][1])
{
int temp = sortFrequency[j][1];
sortFrequency[j][1] = sortFrequency[j+1][1];
sortFrequency[j+1][1] = temp;
temp = sortFrequency[j][0];
sortFrequency[j][0] = sortFrequency[j+1][0];
sortFrequency[j+1][0] = temp;
}
}
//To count frequency of each character
void frequencyCount()
{
int c, d;
//Loops till the length of the inputed data
for(c = 0; c < len; c++)
{
//Loops from 33 to 126 which is the ascii values of required characters range
for(d = 0; d < 84; d++)
{
//If the data in the current position is equal to the acii value
if(data[c] == string[d])
//Frequency 0 position is equal to 33 ascii.
//So 33 is deducted
frequency[d]++;
}//end of for loop
}//end of for loop
}//End of function
//Initializes the frequency array to zero
void initialize()
{
int c;
for(c = 0; c < 84; c++)
frequency[c] = 0;
}
//Read the file which contains data
void readFile()
{
//File pointer created
FILE *fptr;
char ch;
//Fi.
So I am writing a CS code for a project and I keep getting cannot .pdf
1. So I am writing a CS code for a project and I keep getting "cannot open file" I know I put it into
my code, but I don't know why it won't execute after i input ./a.out I put in gcc -Wall
contents3.c and no errors or warnings pop up. So I figure everything is working right. But after
that, I get "Cannot open file". So I am wondering what I can do to get this settled out properly. I
am putting the outcome of the project and my code. Please someone help me!
#include
#include
#include
#include
#include
#include
int len = 0;
int sp = 0;
int lwrcaseCount = 0;
int uprcaseCount = 0;
int digitCount = 0;
int specialCount = 0;
char data[200];
int frequency[84];
int sortFrequency[84][2];
char string[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-
./:;<=>?@[/]^_`{|}~";
//printf(" ");
//To sort based on frequency of characters
/*void sort()
{
int max, loc, temp, x, y;
//Loops till end - 1 position of the frequency array
for(x = 0; x <= 84 - 1; x++)
{
//Initializes the max to the x position of the frequency array assuming it as maximum
max = frequency[x];
//Initializes of loc to x assuming location x contains the maximum value
loc = x;
2. //Loops through x + 1 to length - 1 of frequency array
for(y = x + 1; y <= 95 -1; y++)
{
//If current position of the frequency of the array is greater than the max
//then update the max with the current value of frequency and update the loc
if(frequency[y] > max)
{
max = frequency[y];
loc = y;
}//end of if
}//end of for loop
//If loc is not x then swap
if(loc != x)
{
temp = frequency[x];
frequency[x] = frequency[loc];
frequency[loc] = temp;
}//end of if
//Store the location of the maximum value in the zero column position of row x
sortFrequency[x][0]= loc;
//Store the maximum value in the first column position of row x
sortFrequency[x][1] = max;
}//end of for loop
}//end of function*/
void sort()
{
for(int i = 0; i < 84; i++)
{
sortFrequency[i][0] = i;
sortFrequency[i][1] = frequency[i];
}
for(int i = 0; i < 84-1; i++)
for(int j = 0; j < 84-i-1; j++)
if(sortFrequency[j][1] < sortFrequency[j+1][1])
{
int temp = sortFrequency[j][1];
3. sortFrequency[j][1] = sortFrequency[j+1][1];
sortFrequency[j+1][1] = temp;
temp = sortFrequency[j][0];
sortFrequency[j][0] = sortFrequency[j+1][0];
sortFrequency[j+1][0] = temp;
}
}
//To count frequency of each character
void frequencyCount()
{
int c, d;
//Loops till the length of the inputed data
for(c = 0; c < len; c++)
{
//Loops from 33 to 126 which is the ascii values of required characters range
for(d = 0; d < 84; d++)
{
//If the data in the current position is equal to the acii value
if(data[c] == string[d])
//Frequency 0 position is equal to 33 ascii.
//So 33 is deducted
frequency[d]++;
}//end of for loop
}//end of for loop
}//End of function
//Initializes the frequency array to zero
void initialize()
{
int c;
for(c = 0; c < 84; c++)
frequency[c] = 0;
}
//Read the file which contains data
void readFile()
{
//File pointer created
4. FILE *fptr;
char ch;
//File opened in read mode
fptr = fopen("Data1.txt", "r");
//If fptr is null show error message and terminate
if (fptr == NULL)
{
printf("Cannot open file ");
exit(0);
}//end of if
//read the first character from the file
ch = fgetc(fptr);
//Loops till end of file
while (ch != EOF)
{
//Stores the character in data array
data[len++] = ch;
//reads another character from the file
ch = fgetc(fptr);
}//end of while
//close file
fclose(fptr);
}//End of method
//Display the required information
void dispData()
{
int c;
//Displays the file contents
printf(" Entered Data ");
for(c = 0; c < len; c++)
printf("%c", data[c]);
printf(" %d characters", len - sp);
printf(" lowercase = %d and ", lwrcaseCount);
printf("uppercase = %d and ", uprcaseCount);
printf("digit = %d", digitCount);
printf(" and special = %d ", specialCount);
5. for(c = 0; c < 84; c++)
{
if(c == 0 || sortFrequency[c][1] != sortFrequency[c-1][1])
printf(" Characters occurring %d times : ", sortFrequency[c][1]);
printf("%c ", string[sortFrequency[c][0]]);
}
/*for(c = 0; c < 95; c++)
{
if(sortFrequency[c][1] != 0)
printf(" Character %c occurs %d Times ", sortFrequency[c][0]+33, sortFrequency[c][1]);
}//end of for
printf(" Characters not found in input: ");
for(c = 0; c < 95; c++)
{
if(sortFrequency[c][1] == 0)
//printf(" Character %c occurs %d Times ", sortFrequency[c][0]+33, sortFrequency[c][1]);
printf("%c ", sortFrequency[c][0]);
}//end of for*/
printf(" ");
}//end of function
//Counts lowercase, uppercase, digits, special characters and total characters
void count()
{
int c;
for(c = 0; c < len; c++)
{
if(islower(data[c]))
lwrcaseCount++;
else if (isupper(data[c]))
uprcaseCount++;
else if (isdigit(data[c]))
digitCount++;
else if (ispunct(data[c]))
specialCount++;
else
sp++;
6. }//end of for
}//end of function
//Main function
int main()
{
//Calls the methods
initialize();
readFile();
count();
frequencyCount();
//dispData();
sort();
dispData();
return 0;
} Project overview: Ever wonder what is in a file? Ever wonder how many times each character
actually occurs in the file? Probably not most people never think of things like that. This
program takes an input source and tells the user what characters that source contains. It does not
worry about whitespace (spaces, tabs, newlines) but instead focuses on the printable characters.
These include: 26 upper-case letters, A-Z and 26 lower-case letters, a and 10 digits, 0-9 32
special characters: "#$S&' C [V1 This program reads from standard input until end-of-file.
Once it hits end-of-file, it tells the user what characters it saw, ordered from the most-occurring
character down to the characters that it did not see. As an example, if the user entered the input
below (shown in re the program generates the output shown in blue The Quick Brown Fox
Jumps over the Lazy Old Dog 123456789012345 the quick brown fox jumps over the lazy old
dog 105 characters lower case 67 and uppercase 9 and digit-15 and special 14 Characters
occurring 8 times o Characters occurring 6 times e Characters occurring 4 times h r u d l t
Characters occurring 3 times Characters occurring 2 times a c g i k m n p s v w x y z O 1 2 3 4 5
Characters occurring 1 times b f j q B D F J L Q T 0 6 7 8 9 Characters not found in the input A
C E G H I K M N P R S U V W X Y Z What You Need To Do Create a directory project3 on
your machine. In that directory, create a file named contents.c In contents. c, write the code
needed to find out what characters exist in the input. Make sure that you: o Have a header block
of comments that includes your name and a brief overview of the program. Read from standard
input until you hit end-of-fil o Prints the expected output in a clear, legible format Hint #1: There
are some good built-in character functions in ctype.h that make coding easier. Make sure you
understand the functions isdigit and isupper and islower and ispunct. Hint #2: You can use an
array or arrays to count the number of times you see each printable character Initialize each
7. element to zero and increment a given location when you see that letter) To test your program
with various inputs, you can redirect input so that it comes from a file instead of the keyboard.
As an example, if you wanted to see what the contents of your contents.c file was, you would
type the command /a.out contents .c (using a.exe instead of a.out in Windows
Solution
#include
#include
#include
#include
#include
int len = 0;
int sp = 0;
int lwrcaseCount = 0;
int uprcaseCount = 0;
int digitCount = 0;
int specialCount = 0;
char data[200];
int frequency[84];
int sortFrequency[84][2];
char string[] =
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&'()*+,-
./:;<=>?@[/]^_`{|}~";
//printf(" ");
//To sort based on frequency of characters
/*void sort()
{
int max, loc, temp, x, y;
//Loops till end - 1 position of the frequency array
for(x = 0; x <= 84 - 1; x++)
{
//Initializes the max to the x position of the frequency array assuming it as maximum
max = frequency[x];
//Initializes of loc to x assuming location x contains the maximum value
loc = x;
8. //Loops through x + 1 to length - 1 of frequency array
for(y = x + 1; y <= 95 -1; y++)
{
//If current position of the frequency of the array is greater than the max
//then update the max with the current value of frequency and update the loc
if(frequency[y] > max)
{
max = frequency[y];
loc = y;
}//end of if
}//end of for loop
//If loc is not x then swap
if(loc != x)
{
temp = frequency[x];
frequency[x] = frequency[loc];
frequency[loc] = temp;
}//end of if
//Store the location of the maximum value in the zero column position of row x
sortFrequency[x][0]= loc;
//Store the maximum value in the first column position of row x
sortFrequency[x][1] = max;
}//end of for loop
}//end of function*/
void sort()
{
for(int i = 0; i < 84; i++)
{
sortFrequency[i][0] = i;
sortFrequency[i][1] = frequency[i];
}
for(int i = 0; i < 84-1; i++)
for(int j = 0; j < 84-i-1; j++)
if(sortFrequency[j][1] < sortFrequency[j+1][1])
{
int temp = sortFrequency[j][1];
9. sortFrequency[j][1] = sortFrequency[j+1][1];
sortFrequency[j+1][1] = temp;
temp = sortFrequency[j][0];
sortFrequency[j][0] = sortFrequency[j+1][0];
sortFrequency[j+1][0] = temp;
}
}
//To count frequency of each character
void frequencyCount()
{
int c, d;
//Loops till the length of the inputed data
for(c = 0; c < len; c++)
{
//Loops from 33 to 126 which is the ascii values of required characters range
for(d = 0; d < 84; d++)
{
//If the data in the current position is equal to the acii value
if(data[c] == string[d])
//Frequency 0 position is equal to 33 ascii.
//So 33 is deducted
frequency[d]++;
}//end of for loop
}//end of for loop
}//End of function
//Initializes the frequency array to zero
void initialize()
{
int c;
for(c = 0; c < 84; c++)
frequency[c] = 0;
}
//Read the file which contains data
void readFile()
{
//File pointer created
10. FILE *fptr;
char ch;
//File opened in read mode
fptr = fopen("Data1.txt", "r");
//If fptr is null show error message and terminate
if (fptr == NULL)
{
printf("Cannot open file ");
exit(0);
}//end of if