Forum - C/C++
- problemino in c
djleo (Member )
Pro
Messaggi: 72
Iscritto: 07/03/2010
Il professore ci ha dato un programma da fare e non riesco a fare una cosa.
Mi spiego il programma da fare é:
Scrivere un programma in C che dato un file input.txt stampi in un file output.txt la lunghezza delle parole.
es:
input.txt
Ciao come stai
tutto bene
output.txt
4 4 4
5 4
Il mio problema è che il risultato me lo da su una riga.
posto il sorgente.
Codice sorgente - presumibilmente C++
#include <stdio.h>
#include <string.h>
#define input "input.txt"
#define output "output.txt"
FILE * fin;
FILE * fout;
char str[ 5] ;
int main( ) {
fin= fopen ( input,"r" ) ;
if ( fin== NULL ) {
printf ( "Impossibile trovare file\n " ) ;
exit ( 1) ;
system ( "pause" ) ;
}
fout= fopen ( output,"w" ) ;
do {
do {
fscanf ( fin,"%s" ,& str) ;
fprintf ( fout,"%d " ,strlen ( str) ) ;
} while ( str[ strlen ( str) ] == '\n ' ) ;
fputc ( '\n ' ,fout) ;
} while ( ! feof ( fin) ) ;
fclose ( fin) ;
fclose ( fout) ;
system ( "notepad output.txt" ) ;
}
()
Newbie
Messaggi:
Iscritto:
prova cosi
Codice sorgente - presumibilmente C++
#include <stdio.h>
#include <string.h>
#define input "input.txt"
#define output "output.txt"
FILE * fin;
FILE * fout;
char str[ 5] ;
int main( ) {
fin= fopen ( input,"r" ) ;
if ( fin== NULL ) {
printf ( "Impossibile trovare file\n " ) ;
exit ( 1) ;
system ( "pause" ) ;
}
fout= fopen ( output,"w" ) ;
do {
do {
fscanf ( fin,"%s" ,& str) ;
fprintf ( fout,"%d " ,strlen ( str) ) ;
} while ( str[ strlen ( str) ] == '\n ' ) ;
if ( ( getc ( fin) ) == '\n ' )
fputc ( '\n ' ,fout) ;
} while ( ! feof ( fin) ) ;
fclose ( fin) ;
fclose ( fout) ;
system ( "notepad output.txt" ) ;
}
Matthew (Member )
Expert
Messaggi: 387
Iscritto: 29/01/2007
Codice sorgente - presumibilmente C++
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define input "input.txt"
#define output "output.txt"
FILE * fin;
FILE * fout;
char str[ 50] ;
int main( )
{
int c= 0, i;
fin= fopen ( input,"r" ) ;
if ( fin== NULL )
{
printf ( "Impossibile trovare file\n " ) ;
return 0 ;
getchar ( ) ;
}
fout= fopen ( output,"w" ) ;
while ( fgets ( str,sizeof ( str) ,fin) ! = NULL )
{
for ( i= 0 ; i< strlen ( str) ; i++ )
{
if ( isalnum ( str[ i] ) ) c++ ;
else
{
fprintf ( fout,"%d " , c) ;
c= 0 ;
}
}
fprintf ( fout, "\n " ) ;
}
fclose ( fin) ;
fclose ( fout) ;
printf ( "Operazione completata, premere un tasto per uscire." ) ;
getchar ( ) ;
return 0 ;
}
Questo codice funziona, confrontalo con il tuo, e scroprirai gli errori che avevi commesso! Buon lavoro!
Ultima modifica effettuata da Matthew il 10/04/2010 alle 19:01
djleo (Member )
Pro
Messaggi: 72
Iscritto: 07/03/2010
Grazie dei sugerimenti