Need help in file processing output

This is a discussion on Need help in file processing output within the C Programming forums, part of the General Programming Boards category; thanks for those who helped me. i have another problem but this time i have an output but incorrect character. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    15

    Need help in file processing output

    thanks for those who helped me.
    i have another problem but this time i have an output but incorrect character.
    i have a text file(note pad) containing this sentence:
    "stay extending from ship's mastheads to the side of the ship" but the result is incorrect.
    this is my code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <string.h>
    #include <windows.h>
    
    
    main()
    {
    
    FILE *fp2;
    char nword[15];
    int c, num2;
    
    printf("Enter a word:\t");
    gets(nword);
    
    c = strcmp(nword, "aba");
    if (c == 0)
    {
          fp2 = fopen("def.txt", "r");
         
          num2 = fgetc(fp2);
         
          while(num2 != EOF) 
                {   
                       do
                      {
                      Sleep(50);         
                      putchar(num2);
                      }
                      while(((num2 = fgetc(fp2)) != '1') && ((num2 = fgetc(fp2)) == '\n'));
                }
                fclose(fp2);
                }
                getch();
                }
    thanks.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    What exactly is your while condition trying to do?

    For a start, you call fgetc() TWICE (or maybe only once), depending on whether the first part of the condition evaluates to true or false.

    Code:
    while ( (ch=fgetc(fp)) != EOF ) {
      if ( ch == something ) {
      }
    }
    Your loop should have ONE unconditional file read per iteration of the loop. If you need to do other things with ch, then put them inside the loop.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    15
    my first while loop is for reading all the characters in the text file and the another (do while) is for viewing my desire line in the notepad.

    where will i place the loop?
    Last edited by jerico; 03-09-2011 at 12:49 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memory Leak in AppWizard-Generated Code
    By jrohde in forum Windows Programming
    Replies: 4
    Last Post: 05-19-2010, 04:24 PM
  2. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  3. I'm not THAT good am I?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-19-2006, 10:08 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

Tags for this Thread


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