printf acting strangely

This is a discussion on printf acting strangely within the C Programming forums, part of the General Programming Boards category; Is there any way to clear the output stream?...

  1. #1
    Unregistered
    Guest

    printf acting strangely

    Is there any way to clear the output stream?

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    You can fflush() it, but that only forces characters out from the buffer, onwards to their destination.

    What's up with printf?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Unregistered
    Guest
    Ok here's the problem.... I am wrinting a database system and I am using fscanf to grab records from a file. The problem is that when when i try to printf after the fscanf all the proper string isn't displayed but part of the data pulled from the file is. Here's my code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include "record.h"

    int numRecords;

    int main(void) {
    FILE *fp;
    int choice;

    numRecords = 0;
    initFILE(&fp, "a+");
    countRecords(fp);
    while (1) {
    clrscr();
    printf("Choose on of the following\n"
    "0 - Enter a new record (at the end)\n"
    "1 - Print all the records\n"
    "2 - List accounts with zero balance\n"
    "3 - List accounts with credit balances\n"
    "4 - List accounts with debit balances\n"
    "5 - List accounts with last names that begin with a letter\n"
    "6 - List accounts with first names that begin with a letter\n"
    "7 - List accounts with last names that match a name\n"
    "8 - List accounts with first names that match a name\n"
    "9 - Search by index\n"
    "10 - Delete a record by index (totally removed, leaving no empty file space)\n"
    "11 - Modify a record by index (leaving no empty file space)\n"
    "12 - End of run\n"
    "?: ");
    scanf("%d",&choice);
    switch (choice) {
    case 0:
    printf("\nENTER A NEW RECORD\n");
    addRecord(fp);
    ++numRecords;
    break;
    case 1:
    printf("\nPRINTING ALL RECORDS\n");
    printRecords(fp);
    break;
    case 12:
    fclose(fp);
    printf("\nEXITING PROGRAM...");
    return 0;
    default:
    printf("Invaild input. Please try again\n");
    }
    }

    }

    void anyKey() {
    printf("\nPress any key to continue.");
    getch();
    }

    void initFILE(FILE** fpp, mode m) {
    char* filename ="";

    printf("Please enter the name of the file to open: ");
    scanf("%s",filename);

    if((*fpp=fopen(filename,m)) == NULL) { /* Can't open file */
    printf("ERROR: unable to open %s",filename);
    exit(1);
    }
    else
    printf("Opened file: %s\n",filename);
    }

    void addRecord(FILE* fp) {
    Record client;
    printf("\nPlease enter firstname, lastname, and balance: ");
    scanf("%s%s%f",client.firstname,client.lastname,&c lient.balance);
    fprintf(fp,"%d %s %s %.2f\n", numRecords, client.firstname, client.lastname, client.balance);
    }

    void countRecords(FILE* fp) {
    Record client;

    rewind(fp);
    fscanf(fp,"%d%s%s%f",&client.index, client.firstname, client.lastname, &client.balance);
    while (!feof(fp)) {
    ++numRecords;
    fscanf(fp,"%d%s%s%f",&client.index, client.firstname, client.lastname, &client.balance);
    }
    printf("Number of Records: %d",numRecords);
    anyKey();
    }

    void printRecords(FILE* fp) {
    Record client;
    printf("INDEX FIRST NAME LAST NAME BALANCE\n");
    printf("---------------------------------------------\n");
    rewind(fp);
    fscanf(fp,"%d%s%s%f",&client.index,client.firstnam e,client.lastname,&client.balance);
    while (!feof(fp)){
    printf("%5d %12s %15s %10.2f\n",client.index,client.firstname,client.las tname,client.balance);
    fscanf(fp,"%d%s%s%f",&client.index,client.firstnam e,client.lastname,&client.balance);
    }
    anyKey();
    }

    This only seems to happen after countRecords is run and the file actually contains records in it. Any thoughts? I think there is a problem with the countRecords() function.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    First off, you shouldn't loop using this as your control:
    >while (!feof(fp))

    You should be calling fscanf() and testing it for EOF.


    I'll continue looking, but in the meantime, make this change.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Unregistered
    Guest
    Is there a way to read an entire line from the file at a time without using the function fread()?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    fgets() gets a line of text, upto a given length.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    > char* filename ="";
    > printf("Please enter the name of the file to open: ");
    > scanf("%s",filename);
    Your code is broken here
    Although you initialise the pointer (good), you initialise it with something which cant hold too many chars (none at all). In addition, it may also be non-writable, though it is in your case

    char filename[100];
    printf("Please enter the name of the file to open: ");
    scanf("%s",filename);


    > while (!feof(fp)) {
    The dreaded !feof "bug"
    See my "copy file" example here

    You could write
    Code:
    while ( fscanf( fp, "%d%s%s%f",
            &client.index, client.firstname,
            client.lastname, &client.balance) != EOF  ) {
    Which loops until end of file

    Or perhaps with some error checking
    Code:
    while ( fscanf( fp, "%d%s%s%f",
            &client.index, client.firstname,
            client.lastname, &client.balance) == 4  ) {
    Which loops until either EOF, or a bad record is found

    Noting your first bug, are client.firstname and client.lastname arrays (char[]) or pointers (char*). Because they need to be arrays.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making it portable.....?
    By ShadeS_07 in forum C Programming
    Replies: 11
    Last Post: 12-24-2008, 08:38 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. segmentation fault upon reload
    By yabud in forum C Programming
    Replies: 8
    Last Post: 12-18-2006, 05:54 AM
  4. Simple C question: user input to repeat a loop
    By evernaut in forum C Programming
    Replies: 2
    Last Post: 11-18-2006, 08:23 AM
  5. Drawing tables in C
    By stanoman in forum C Programming
    Replies: 5
    Last Post: 10-09-2003, 10:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21