Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

reading from file

manggamangga Member Posts: 16
im having trouble reading info from a file. wat i wanted was the 3 items: name, tely n address to be in the same line
eg. John Doe 11223344 Happy Village 9563 .

[code]/* Info Input */
#include

main()
{
long int tely;
char name[ 15 ], address[ 40 ];
FILE *tdPtr;

if ( ( tdPtr = fopen( "tel_dir.dat", "w" ) ) == NULL )
printf( "Nggak mau buka neng
" );

else
{
printf( "Press [ Ctrl + Z ] to end input.

" );

printf( "Enter Name: " );
gets( name );

printf( "Telephone number: " );
scanf( "%ld", &tely );

fflush( stdin );
printf( "Address: " );
gets( address );

printf( "
*********************************

" );

while ( !feof( stdin ) )
{
fprintf( tdPtr, "%s
%ld
%s
", name, tely, address );

fflush( stdin );
printf( "Enter Name: " );
gets( name );

printf( "Telephone number: " );
scanf( "%ld", &tely );

fflush( stdin );
printf( "Address: " );
gets( address );

printf( "
*********************************

" );
}

fclose( tdPtr );
}

return 0;
}[/code]

this code is okay. the info is stored like this:

John Doe
11223344
Happy Village 9563

i file it like this so that i can strcmp them easily( another code ).

[code]/* Reading From File */
#include

main()
{
long int tely;
char name[ 15 ], address[ 40 ];
FILE *tdPtr;

if ( ( tdPtr = fopen( "tel_dir.dat", "r" ) ) == NULL )
printf( "Nggak mau buka neng
" );

else
{

fgets( name, 15, tdPtr );
fscanf( &tely, 8, tdPtr );
fgets( address, 40, tdPtr );

while ( !feof( tdPtr ) )
{
printf( "%s %ld %s", name, tely, address );
fgets( name, 15, tdPtr );
fscanf( &tely, 8, tdPtr );
fgets( address, 40, tdPtr );
}

fclose( tdPtr );
}

return 0;
}[/code]

at best the output was like how it was stored( not using above code ). and please explain how 2 use fgets n fputs. seems like fgets puts '
' to every string it reads. how can i remove/ignore it.

Comments

  • stoberstober Member Posts: 9,765 ✭✭✭
    : at best the output was like how it was stored( not using above code ). and please explain how 2 use fgets n fputs. seems like fgets puts '
    ' to every string it reads. how can i remove/ignore it.
    :
    :

    [blue]You are correct, fgets() adds the '
    ' to the end of the string. This will remove it. [blue]
    [code]
    int main()
    {
    char buf[255];
    fgets(buf,sizeof(buf),stdin);

    // remove trailing '
    '
    int len = strlen(buf)-1;
    if( buf[len] == '
    ')
    buf[len] = 0;

    return 0;
    }
    [/code]

  • manggamangga Member Posts: 16
    doesnt seem to work. adds some fixed numbers instead.

    [code]#include

    main()
    {
    long int tely;
    int leng;
    char name[ 15 ], address[ 40 ];
    FILE *tdPtr;

    clrscr();

    if ( ( tdPtr = fopen( "tel_dir.dat", "r" ) ) == NULL )
    printf( "Nggak mau buka neng
    " );

    else
    {

    fgets( name, 15, tdPtr );
    leng = strlen( name ) - 1;
    if( name[ leng ] == '
    ' )
    {
    name[ leng ] = 0;
    }

    fscanf( &tely, 7, tdPtr );
    fgets( address, 40, tdPtr );

    while ( !feof( tdPtr ) )
    {
    leng = 0;
    printf( "%s %ld %s", name, tely, address );
    fgets( name, 15, tdPtr );
    leng = strlen( name ) - 1;
    if( name[ leng ] == '
    ' )
    {
    name[ leng ] = 0;
    }

    fscanf( &tely, 7, tdPtr );
    fgets( address, 40, tdPtr );

    }

    fclose( tdPtr );
    }

    return 0;
    }[/code]
  • stoberstober Member Posts: 9,765 ✭✭✭
    : doesnt seem to work. adds some fixed numbers instead.
    :

    maybe your buffers aren't large enough. increase each of them by two bytes to allow for the '
    ' and trailing null byte.
Sign In or Register to comment.