I am having a hard time with getting my code to write the txt file. Any ideas?
<!DOCTYPE html>
<html>
<head>
<title>Song Organizer</title>
<meta name="Student" content="Student" />
<!-- create a song organizer script -->
</head>
<body>
<h1>Song Organizer</h1>
<?php
// create a song organizer script
$filename = 'songs.txt';
$filepath = __DIR__ . '/' . $filename;
$songs = [];
if (isset($_GET['action']))
{
if ((file_exists("SongOrganizer2/songs.txt"))
&& (filesize("SongOrganizer2/songs.txt")!= 0))
{$SongArray = file("SongOrganizer2/songs.txt");
switch ($_GET['action'])
{
case 'Remove Duplicates';
$SongArray = array_unique($SongArray);
$SongArray = array_values($SongArray);
break;
case 'Sort Ascending':
sort($SongArray);
break;
case 'Shuffle':
shuffle($SongArray);
break;
} // End of the switch statement
if (count($SongArray) > 0)
{
$NewSongs = implode($SongArray);
$SongStore = fopen("SongOrganizer2/songs.txt","wb");
if ($SongStore === false)
echo "There was an error updating the song file\n";
else
{fwrite($SongStore, $NewSongs);
fclose($SongStore);
}
} else
unlink("SongOrganizer2/songs.txt");
}
}
if(isset($_POST['submit']))
$SongToAdd = stripslashes($_POST['SongName']) . "\n";
$ExistingSongs = array();
if (file_exists("SongOrganizer2/songs.txt")
&filesize("SongOrganizer2/songs.txt")> 0)
{ $ExistingSongs = file("SongOrganizer2/songs.txt");}
if (in_array($SongToAdd, $ExistingSongs))
{echo "<p>The song you entered already exists!<br />\n";
echo "Your song was not added to the list.</p>";
}
else
{
$SongFile = fopen("SongOrganizer2/songs.txt","ab");
if ($SongFile === false)
echo "There was an error saving your message!\n";
else
{
fwrite($SongFile, $SongToAdd);
fclose($SongFile);
echo "Your song has been added to the list.\n";
}
}
if ((!file_exists("SongOrganizer2/songs.txt"))
|| (filesize("SongOrganizer2/songs.txt")== 0))
echo "<p>There are no songs in the list.</p>\n";
else
{
$SongArray = file("SongOrganizer2/songs.txt");
echo "<table border=\"1\" width=\"100%\" style=\"background-color:lightgray\">\n";
foreach ($SongArray as $Song) {
echo "<tr>\n";
echo "<td>" . htmlentities($Song) ."</td>";
echo "</tr>\n";}
echo "</table>\n";
}
?>
<p>
<a href="SongOrganizer2.php?action=Sort%20Ascending">
Sort Song List </a><br />
<a href="SongOrganizer2.php?action=Remove%20Duplicates">
Remove Duplicate Songs </a><br />
<a href= "SongOrganizer2.php?action=Shuffle">
Randomize Song List </a><br />
</p>
<form action="SongOrganizer2.php" method="post">
<p>Add a New Song</p>
<p>Song Name: <input type="text" name="SongName"/></p>
<p><input type="submit" name = 'submit'
value="Add Song to List" />
<input type="reset" name="reset" value="Reset Song Name" /></p>
</form>
</body>
</html>.
I am having problems getting the correct output- Here are the instruct (1).pdf
1. I am having problems getting the correct output. Here are the instructions:
For this assignment, you will write a program that will accept up to 2048 characters from stdin
and emit a report to stdout listing each unique word found in the input, the offset of the
beginning of the word from the beginning of the input, the number of times the word was in the
input and the word itself. You should store the input characters into a character array as your
working buffer.
A word is defined as any sequence of non-whitespace characters.
The input will terminate with an EOF if there are less than 2048 characters, but your program
shall accept no more than 2048.
Your program shall record the beginning and end of each word in the buffer and use the buffer
itself as the storage for the content of each word. The report needs to be generated from the
buffer
and the index to the words in the buffer.
You are not allowed to declare or use any global variables. All variables must be locally scoped.
You may however add any additional #define directives you deem needed or helpful.
Here is my code so far:
.......................................................................................................................
#include <stdio.h>
#include <ctype.h>
#include "string_index.h"
int main(void) {
// variable declartaions
char buffer[BUFFER_SIZE]; // begins 2048 buffer
int word_index_array[WORD_LIMIT][2]; // index by array
int word_counts[WORD_LIMIT]; // number of occurances
const char *word_index[WORD_LIMIT][2]; // index by pointer
int words_found; // shows the words found
2. // initialize storage
for (int i = 0; i < BUFFER_SIZE; i++) {
buffer[i] = '0';
}
/* ***** DO NOT CHANGE ANY CODE BEYOND THIS POINT IN main() ****** */
initialize_buffer(buffer);
words_found = index_buffer_by_ptr(buffer, word_index, word_counts);
print_report_by_ptr(buffer, word_index, word_counts, words_found);
words_found = index_buffer_by_array(buffer, word_index_array, word_counts);
print_report_by_array(buffer, word_index_array, word_counts, words_found);
} // end main
/* ***** YOUR FUNNCTION DEFINTIONS BELOW ***** */
int initialize_buffer(char buffer[]) {
int i = 0;
int c;
while (i < BUFFER_SIZE - 1 && (c = getchar()) != EOF) {
buffer[i++] = c;
}
buffer[i] = '0';
if (i == 0) {
return -1;
} else {
return i;
3. }
};
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] ) {
int word_cnt = 0;
int len = strlen(buffer);
int i = 0;
int word_beg = 0;
int word_end = 0;
int is_word = 0;
for (i = 0; i < len; i++) {
if (!is_word && !isspace(buffer[i])) {
word_beg = i;
is_word = 1;
} else if (is_word && isspace(buffer[i])) {
word_end = i - 1;
is_word = 0;
int j;
for (j = 0; j < word_cnt; j++) {
if (word_end - word_beg != index[j][1] - index[j][0]) {
continue;
}
int k;
for (k = index[j][0]; k <= index[j][1]; k++) {
4. if (buffer[word_beg + k - index[j][0]] != buffer[k]) {
break;
}
}
if (k == index[j][1] + 1) {
w_counts[j]++;
break;
}
}
if (j == word_cnt) {
index[j][0] = word_beg;
index[j][1] = word_end;
w_counts[word_cnt] = 1;
word_cnt++;
}
}
}
return word_cnt;
};
int find_word_by_array(int word_beg, const char buf[], int index[][2]) {
for (int i = 0; index[i][WD_BGN] != -1; i++) {
if (strncmp(&buf[word_beg], &buf[index[i][WD_BGN]], index[i][WD_END] -
index[i][WD_BGN] + 1) == 0) {
return i;
5. }
}
return NOT_FOUND;
};
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*sn", i, index[i][WD_BGN], counts[i], index[i][WD_END] -
index[i][WD_BGN] + 1, &buf[index[i][WD_BGN]]);
}
};
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] ) {
int i = 0;
int w_count = 0;
while (*buf != '0') {
if (!isspace(*buf)) {
// Found a word
const char *word_beg = buf;
while (!isspace(*buf) && *buf != '0') {
buf++;
}
const char *word_end = buf - 1;
int index_found = find_word_by_ptr(word_beg, index);
if (index_found == NOT_FOUND) {
// New word found
6. index[w_count][WD_BGN] = word_beg;
index[w_count][WD_END] = word_end;
word_counts[w_count] = 1;
w_count++;
} else {
// Existing word found
word_counts[index_found]++;
}
} else {
buf++;
}
}
return w_count;
};
int find_word_by_ptr(const char *beg, const char *index[][2]) {
int i, j;
for (i = 0; index[i][WD_END] != NULL; i++) {
if (index[i][WD_BGN] == NULL) {
continue;
}
if (strlen(beg) != index[i][WD_END] - index[i][WD_BGN]) {
continue;
}
for (j = 0; j < index[i][WD_END] - index[i][WD_BGN]; j++) {
if (*(beg + j) != *(index[i][WD_BGN] + j)) {
break;
}
}
if (j == index[i][WD_END] - index[i][WD_BGN]) {
return i;
}
}
7. return NOT_FOUND;
};
void print_report_by_ptr(const char *buf, const char *index[][2], int counts[], int word_cnt) {
for (int i = 0; i < word_cnt; i++) {
printf("%d(%2d): %d %.*sn", i, (int)(index[i][WD_BGN] - buf), counts[i],
(int)(index[i][WD_END] - index[i][WD_BGN] + 1), index[i][WD_BGN]);
}
};
.........................................................................................................................
Ande here is the header file:
..........................................................................................................................
#include <stdio.h>
#include <ctype.h>
#ifndef STRING_INDEX_HEADER
#define STRING_INDEX_HEADER
#define BUFFER_SIZE 2048
#define WORD_LIMIT 100
#define NOT_FOUND -1
#define WD_BGN 0
#define WD_END 1
// Prototypes
/*
initialize_buffer:
Fills the given array with characters from stdin until either EOF or
8. 2048 characters have been loaded.
Returns the total number of characters loaded and -1 if no characters loaded.
*/
int initialize_buffer(char buffer[]);
/********************************************************
Functions that work with arrays
*******************************************************
*/
/*
index_buffer_by_array:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending array indexes for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_array(const char buffer[], int index[][2], int w_counts[] );
/*
find_word_by_array:
Determines if the word in buf[] beginning at index word_beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_array(int word_beg, const char buf[], int index[][2]);
9. /*
print_report_by_array:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of array indexes for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_array(const char buf[], int index[][2], int counts[], int word_cnt);
/*******************************************************
Functions that work with pointers
********************************************************/
/*
index_buffer_by_ptr:
Indexes the beginning and end of each unique word (string of non-whitespace characters).
The index is a list of beginning and ending char pointers for each unique word found.
Duplicate words are counted in w_counts array.
Returns the total number of unique words found in the character buffer.
*/
int index_buffer_by_ptr(const char * buf, const char * index[][2], int word_counts[] );
/*
find_word_by_ptr:
10. Determines if the word in beginning at the char * beg is already indexed.
A word terminates with the first whitespace character.
Returns the index number for the word if found, otherwise returns NOT_FOUND.
*/
int find_word_by_ptr(const char * beg, const char * index[][2]);
/*
print_report_by_ptr:
Prints to stdout a report giving the word index number, offset of first letter from
beginning of the buffer, the word count, and the word itself. This function expects index
to be an array of char pointers for the beginning and end of each word.
Example output for buffer containing the characters in quotes "all the all"
0( 0): 2 all
1( 4): 1 the
*/
void print_report_by_ptr(const char * buf, const char * index[][2], int counts[], int word_cnt);
#endif
...........................................................................................................................
Here is my current output (NOT WHAT I AM LOOKING FOR):
0( 0): 1 How
1( 4): 1 many
2( 9): 1 wood
3(14): 1 chucks
4(21): 1 would
5(27): 1 chuck
6(33): 1 wood
7(38): 1 if
8(41): 1 a
9(43): 1 wood
11. 10(48): 1 chuck
11(54): 1 could
12(60): 1 chuck
13(66): 1 wood.
0( 0): 1 How
1( 4): 1 many
2( 9): 3 wood
3(14): 1 chucks
4(21): 1 would
5(27): 3 chuck
6(38): 1 if
7(41): 1 a
8(54): 1 could
My array functions almost works, it is just cutting off the last word of the input and it counts
words with an uppercase letter as different than a lowercase (e.g., 0(0) 1 All, 1(4) 1 all).
Everything else about that is correct. My ptr functions are not working correctly. I am trying to
show each unique word once with the count next to them. We are not using c string in this so
don't use 'strcmp' please.