![]() |
| | #1 |
| Registered User Join Date: Mar 2007
Posts: 28
| help with text input Code: #include <stdio.h>
#include <stdlib.h>
int main()
{
int alpha = 0;
int digit = 0;
int punct = 0;
int wspace = 0;
FILE* sp1;
int input;
while ((input = fgetc(sp1)) != EOF)
{
if (isalpha(input))
alpha++;
else if (isdigit(input))
digit++;
else if (ispunct(input))
punct++;
else if (isspace(input))
wspace++;
}//while
printf("alphabetic character: %d \n digits: %d \n punctuations: %d \n whitespace characters: %d \n", alpha, digit, punct, wspace);
system("PAUSE");
return 0;
}
Also since i dont know how to test it i dont know if my coding is correct. Feel free to nitpick the coding as well ^^. |
| Alphawaves is offline | |
| | #2 |
| Registered User Join Date: Jul 2006
Posts: 158
| the variable sp1 is the name of the variable, not the name of the file. to work with the file, you have to use the function "fopen" and subsequently "fclose". here is the modified source code: Code: #include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
int alpha = 0;
int digit = 0;
int punct = 0;
int wspace = 0;
FILE* sp1;
sp1 = fopen ("<insertfilename>", "r");
int input;
while ((input = fgetc(sp1)) != EOF)
{
if (isalpha(input))
alpha++;
else if (isdigit(input))
digit++;
else if (ispunct(input))
punct++;
else if (isspace(input))
wspace++;
}//while
printf("alphabetic character: %d \n digits: %d \n punctuations: %d \n whitespace characters: %d \n", alpha, digit, punct, wspace);
fclose (sp1);
return 0;
}
|
| divineleft is offline | |
| | #3 |
| Registered User Join Date: Mar 2007
Posts: 218
| You need to open the file first. I think you're not developing the program the right way, so I'll do it and post each version so you can see how to incrementally build a program. 1 - Start with a skeleton program: Code: #include <stdio.h>
int main( void ) {
return 0;
}
Code: #include <stdio.h>
int main( void ) {
FILE *fp = fopen( "test.txt", "r" );
if ( fp ) {
int ch;
while ( ( ch = fgetc( fp ) ) != EOF ) {
fputc( ch, stdout );
}
fclose( fp );
} else {
perror( "error opening the file" );
}
return 0;
}
Code: #include <stdio.h>
int main( void ) {
FILE *fp = fopen( "test.txt", "r" );
if ( fp ) {
int ch;
int n = 0;
while ( ( ch = fgetc( fp ) ) != EOF ) {
++n;
}
printf( "total characters: %d\n", n );
fclose( fp );
} else {
perror( "error opening the file" );
}
return 0;
}
Code: #include <stdio.h>
#include <ctype.h>
int main( void ) {
FILE *fp = fopen( "test.txt", "r" );
if ( fp ) {
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
} else if ( isdigit( ch ) ) {
++ndigit;
} else if ( ispunct( ch ) ) {
++npunct;
} else if ( isspace( ch ) ) {
++nspace;
}
}
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
fclose( fp );
} else {
perror( "error opening the file" );
}
return 0;
}
Code: #include <stdio.h>
#include <ctype.h>
void process_file( FILE *fp );
int main( void ) {
FILE *fp = fopen( "test.txt", "r" );
if ( fp ) {
process_file( fp );
fclose( fp );
} else {
perror( "error opening the file" );
}
return 0;
}
void process_file( FILE *fp ) {
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
} else if ( isdigit( ch ) ) {
++ndigit;
} else if ( ispunct( ch ) ) {
++npunct;
} else if ( isspace( ch ) ) {
++nspace;
}
}
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
}
Code: #include <stdio.h>
#include <ctype.h>
void process_file( FILE *fp );
int main( int argc, char *argv[] ) {
if ( argc > 1 ) {
FILE *fp = fopen( argv[1], "r" );
if ( fp ) {
process_file( fp );
fclose( fp );
} else {
perror( "error opening the file" );
}
} else {
fprintf( stderr, "usage: prog <filename>\n" );
}
return 0;
}
void process_file( FILE *fp ) {
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
} else if ( isdigit( ch ) ) {
++ndigit;
} else if ( ispunct( ch ) ) {
++npunct;
} else if ( isspace( ch ) ) {
++nspace;
}
}
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
}
Code: #include <stdio.h>
#include <ctype.h>
int process_file( FILE *fp );
int main( int argc, char *argv[] ) {
if ( argc > 1 ) {
FILE *fp = fopen( argv[1], "r" );
if ( fp ) {
if ( !process_file( fp ) ) {
perror( "error reading from the file" );
}
fclose( fp );
} else {
perror( "error opening the file" );
}
} else {
fprintf( stderr, "usage: prog <filename>\n" );
}
return 0;
}
int process_file( FILE *fp ) {
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
int rc = 0;
if ( fp != NULL ) {
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
} else if ( isdigit( ch ) ) {
++ndigit;
} else if ( ispunct( ch ) ) {
++npunct;
} else if ( isspace( ch ) ) {
++nspace;
}
}
if ( !ferror( fp ) ) {
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
rc = 1;
}
}
return rc;
}
Code: /*
File - prog.c
Author - D. Burke (Noir)
Count alphabetic, digit, punctuation, and
whitespace characters in a user supplied file
*/
#include <stdio.h>
#include <ctype.h>
int process_file( FILE *fp );
int main( int argc, char *argv[] ) {
if ( argc > 1 ) {
FILE *fp = fopen( argv[1], "r" );
if ( fp ) {
if ( !process_file( fp ) ) {
// failure means a stream error or bad file
perror( "error reading from the file" );
}
fclose( fp );
} else {
perror( "error opening the file" );
}
} else {
fprintf( stderr, "usage: prog <filename>\n" );
}
return 0;
}
int process_file( FILE *fp ) {
int ch;
int nalpha = 0;
int ndigit = 0;
int npunct = 0;
int nspace = 0;
// assume failure
int rc = 0;
if ( fp != NULL ) {
while ( ( ch = fgetc( fp ) ) != EOF ) {
if ( isalpha( ch ) ) {
++nalpha;
} else if ( isdigit( ch ) ) {
++ndigit;
} else if ( ispunct( ch ) ) {
++npunct;
} else if ( isspace( ch ) ) {
++nspace;
}
}
if ( !ferror( fp ) ) {
// only produce output if there are no errors
printf( "alphabetic characters: %d\n", nalpha );
printf( "digit characters: %d\n", ndigit );
printf( "punctuation characters: %d\n", npunct );
printf( "whitespace characters: %d\n", nspace );
rc = 1;
}
}
return rc;
}
|
| Noir is offline | |
| | #4 |
| Registered User Join Date: Mar 2007
Posts: 28
| That was an awesome explaination noir, thanks. What about my integer classification question, can anyone answer that? |
| Alphawaves is offline | |
| | #5 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| > What about my integer classification question, can anyone answer that? > input is of class int Your book was just telling you what data type to use for the numerical data. Your program will differentiate between whitespace, digits, characters and punctuation if you use the right functions to look for those things. All input to a console program is textual in nature... integers aren't normally sent to the console (except in special cases), so if necessary, you would convert a string to an integer. In a counting program such as this one, it isn't necessary. |
| whiteflags is offline | |
| | #6 | |
| Registered User Join Date: Mar 2007
Posts: 218
| Quote:
The reason you have to use an int for fgetc() is because of EOF. fgetc() returns either a character in the range of unsigned char or EOF. unsigned char is guaranteed to be positive, and EOF is guaranteed to be negative, so fgetc() can't return an unsigned char. It also can't return a signed char or a bunch of legitimate characters won't be returned right. So fgetc() returns an int because characters are just small integers. int can hold the full range of an unsigned char and the negative value of EOF. | |
| Noir is offline | |
| | #7 |
| Registered User Join Date: Mar 2007
Posts: 28
| Gotcha, after looking at closer, divines code doesnt compile. Looking back at it though i dont see a need to structure it like that, because the way noir has it structured makes alot more sense. The only thing i didnt understand was the addition of the "argument stuff" i dont think im familiar with those commands. I probably wont split my program into two different blocks but well see. So how do i test this? Do i need to make a .txt file in the same directory? |
| Alphawaves is offline | |
| | #8 | ||
| Registered User Join Date: Mar 2007
Posts: 218
| Quote:
Code: C:\>prog.exe testfile.txt Code: {"prog.exe","testfile.txt",NULL}
Code: int main( void ) {
FILE *fp = fopen( "C:\\worker\\testfiles\\test.txt", "r" );
Quote:
| ||
| Noir is offline | |
| | #9 | |
| Registered User Join Date: Jul 2006
Posts: 158
| Quote:
if it's segfaulting it's because the file specified doesn't exist. if you don't want it to segfault, you need to check if the stream is actually open before reading from it. that or specify a file that actually exists | |
| divineleft is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Choosing a variable based on user text input. | Compiling... | C++ Programming | 7 | 11-01-2005 01:21 AM |
| Parsing Text File and gathering input variables | azamsharp1 | C Programming | 2 | 10-26-2005 08:43 AM |
| Unknown Memory Leak in Init() Function | CodeHacker | Windows Programming | 3 | 07-09-2004 09:54 AM |
| mygets | Dave_Sinkula | C Programming | 6 | 03-23-2003 07:23 PM |
| text input buffer clearing | red_Marvin | C++ Programming | 4 | 03-20-2003 03:17 PM |