I'm pretty new to this forum, although I have run into cprogramming from time to time when Googling. That may make me a lurker. Anyways:
I need help with a homework assignment, which I haven't been able to make much headway with. No, I am not asking someone to do the whole assignment for me. I'm looking for someone to give me some code that points me in the right direction.
My Work:
I have several theories, each of which seems more unlikely than the last. I first created a program to read the entire text file, character by character into terminal. I then made the program count the number of spaces in the text file. It was my hope that I could modify this program to create a list of all the words that appeared in the text file, since it could recognize spaces. Here is my code:
Code:
#include <stdio.h>
int spaces = 0;
int main ( int argc, char *argv[] )
{
if ( argc != 2 )
{
printf( "usage: %s filename", argv[0] );
}
else
{
FILE *file = fopen( argv[1], "r" );
if ( file == 0 )
{
printf( "Could not open file\n" );
}
else
{
int x;
int i;
while ( ( x = fgetc( file ) ) != EOF )
{
printf( "%c", x );
if (x ==' ')
{
spaces++;
}
}
}
fclose( file );
printf ( "\n\n The number of spaces contained within the file is: %d", spaces );
}
}
I don't know if this approach will work or not.
I also attempted to modify this example code to halt at each space, and concantate the characters found so far into another variable/string "z".
Code:
#include <stdio.h>
#include <string.h>
int main() {
FILE *file;
char *c; /* make sure it is large enough to hold all the data! */
char b[200];
char *z;
char *d;
int n;
file = fopen("zzz.txt", "r");
if(file==NULL) {
printf("Error: can't open file.\n");
return 1;
}
else {
printf("File opened successfully.\n");
// n = fread(c, 1, 10, file); /* passing a char array,
// reading 10 characters */
// c[n] = '\0'; /* a char array is only a
// string if it has the
// null character at the end */
// printf("%s\n", c); /* print out the string */
// printf("Characters read: %d\n\n", n);
//
// fclose(file); /* to read the file from the beginning, */
// /* we need to close and reopen the file */
// file = fopen("numbers.txt", "r");
while(1) { /* keep looping... */
c = fgetc(file);
if(c!=" ") {
z = strcat(z,c);
printf("%z\n", c);
}
else {
continue; /* ...break when EOF is reached */
}
if(c==EOF);
{
break;
}
}
// n = fread(d, 1, 10, file);
/* passing a char pointer this time - 10 is irrelevant */
// printf("%s\n", d);
// printf("Characters read: %d\n\n", n);
fclose(file);
return 0;
}
}
It was my hope that stopping at each space and appending the letters found so far into a variable/string/char would create a list of words for me - not worrying about whether it was unique yet or not. Then I could write some more code to check how many times the contents of each line appeared uniquely, thus generating my list of words and the number of times they appear.
Somebody's going to tell me that I'm trying to reinvent maps or structs or that I can't typecast to save my life. In truth, I don't fully understand these functions too well. I happily understand every function in the second chunk of code up there, and I'll cheerfully learn about whichever functions can help me.
That said, if you give me code, and you do 1, 3, or 4 differently, please explain a little bit. Because I'm familiar with things like fopen, fclose, only.
1. Open a text file.
2. Parse the file for words. <--- need help
3. Print to terminal.
4. Close the file.
Any help, including criticism at my approach, is appreciated.
AA.