Thread: problem with file input output

  1. #1
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118

    problem with file input output

    i am using this program to read string from a file and removing vowels from it. But i am having problems with writing the new string in the file!

    please help me. I am using Turbo C/C++ IDE

    Code:
    /* vowel_remover.c */
    
    #include<stdio.h>
    #include<conio.h>
    #define not_vowel(ch) (ch!='A'||ch!='a'||ch!='E'||ch!='e'||ch!='I'||ch!='i'||ch!='O'||ch!='o'||ch!='U'||ch!='u')
    
    void main(void)
    {
     FILE *fp;
     char ch;
     fp=fopen("F:/Testfile.txt","a+");
     if(fp==NULL)printf("file not found");
     while(1)
    	{
    	 ch=fgetc(fp);
    	 if(ch==EOF)break;
    	 putch(ch);
    	 if(not_vowel(ch))
    	 fprintf(fp,"%c",ch);
    	}
     fclose(fp);
     getch();
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Two suggestions:

    1) Define's that are anything but simple literal replacements, are just two doors down from the entrance to HELL. Avoid them, or be ready to be burned.

    2) Your if(notvowel()) looks dodgy - without a return type, what does if(functionName()), resolve to, a zero or something else?

    "Hetty, git the shotgun. I see a DEFINE that needs a'killing."

  3. #3
    Registered User
    Join Date
    May 2010
    Location
    Chennai
    Posts
    4
    Code:
    <siva> Fri May 21 17:47:55 2010
    
    /* vowel_remover.c */
    
    #include<stdio.h>
    #include <curses.h>
    
    #define not_vowel(ch) (ch!='A'||ch!='a'||ch!='E'||ch!='e'||ch!='I'||ch!='i'||ch!='O'||ch!='o'||ch!='U'||ch!='u')
    
    main()
    {
      FILE *fp;
      char c;
      char buffer[256];
      fp = fopen("wordlist.txt","a+");
      if(fp == NULL)printf("file not found");
      while((c = fgetc(fp))!= EOF){
        {
          if(not_vowel(c))
            printf("%c",c);
          else
            ;
        }
      }

  4. #4
    Registered User
    Join Date
    May 2010
    Location
    Chennai
    Posts
    4
    Code:
    The problem with ur code is 
    #define not_vowel(ch) (ch!='A'||ch!='a'||ch!='E'||ch!='e'||ch!='I'||ch!='i'||ch!='O'||ch!='o'||ch!='U'||ch!='u')
    it should be of
    
    #define not_vowel(ch) (ch!='A'&& ch!='a'&& ch!='E'&& ch!='e'&& ch!='I'&& ch!='i'\
    && ch!='O'&& ch!='o'&& ch!='U'&& ch!='u')'
    
    also with some minor mistakes . For your reference the code is given below
    -------------------------------------------------------------------------
    /* vowel_remover.c */
    
    #include<stdio.h>
    #include <curses.h>
    
    #define not_vowel(ch) (ch!='A'&& ch!='a'&& ch!='E'&& ch!='e'&& ch!='I'&& ch!='i'\
    && ch!='O'&& ch!='o'&& ch!='U'&& ch!='u')
    
    main()
    {
      FILE *fp;
      char c;
      char buffer[256];
      fp = fopen("wordlist.txt","a+");
      if(fp == NULL)printf("file not found");
      while((c = fgetc(fp))!= EOF){
        {
          if(not_vowel(c))
            printf("%c",c);
          else
            ;
        }
      }
      fclose(fp);
    }

  5. #5
    Infant of C
    Join Date
    May 2010
    Location
    Karachi, Pakistan
    Posts
    118
    i am sorry! I was unable to explain my problem properly!

    the problem is not with vowels but it is with appending the out put in the file!

    I want to append the new string in the file but it is not working!

    I mean the code is not appending the new text in file although i have used "a+" mode.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Is it replacing instead? You may need to fseek to the end of the file first.

    EDIT: And re-reading, this time with comprehension: you're printing to the screen not the file anyway. But as my esteemed colleague says below, you won't be able to read and write to the file at the same time. You need to do all the reading, then all the writing.
    Last edited by tabstop; 05-21-2010 at 09:38 AM.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Well the thing is you're not necessarily allowed to use a stream like the way you are (read a byte, then append a byte).

    The idea of a bidrectional stream is that you would use the file one way and one way only until you do a rewind() operation, then you can use the stream to append or write or whatever. So, say, as a read-from stream, and then call rewind(fp); and do your appending.

    I've always tried to avoid bi-directional streams because they are confusing and you do not need them. You could just as easily rewrite the whole damn file because the implementation is going to force you to keep everything in memory one way or another.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you help me about tolower() in file
    By nctar in forum C Programming
    Replies: 7
    Last Post: 05-12-2010, 10:04 AM
  2. File Copy problem
    By reenact12321 in forum C Programming
    Replies: 2
    Last Post: 02-18-2010, 03:08 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Outputting to a File Modified Text
    By alpha in forum C++ Programming
    Replies: 8
    Last Post: 11-24-2003, 08:39 PM
  5. problem with output
    By Garfield in forum C Programming
    Replies: 2
    Last Post: 11-18-2001, 08:34 PM