Hi I'm Indrajeet. I was writing a program in VC++ 2010 Express for file manipulation, and after finally getting all the syntax errors out,my code looks lie this:

/*********************************************************************
This is a program for file manipulation in VC++
  Created by Indrajeet Roy
*********************************************************************/


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

void main()
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);
	//Print the Menu:
	printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
	printf("\nEnter '4' to exit");
	scanf("%d",&ch);
	
	
	//Actual Processing:
	do
	{
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}
		
	}while(ch!=4);
}
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	fclose(fw);
}

void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(fw==NULL)
		abort();
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	fclose(fw);
}

void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
		abort();
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	fclose(fw);
}

The problems are
1)When the code runs,the write() does not pass control back after I press the *.
2)In the read(),it displays an unending loop of printing "3" for some reason!!

Please help.
Thanks
Indrajeet Roy

I believe in windows the EOF is a two character combination, so the read loop will never end because "ch" cannot be evaluated to EOF

This wiki article suggests passing the "fgetc(fw)" to an integer, comparing that to EOF, then converting it to a character to be stored in "ch"

Hi I'm Indrajeet. I was writing a program in VC++ 2010 Express for file manipulation, and after finally getting all the syntax errors out,my code looks lie this:

/*********************************************************************
This is a program for file manipulation in VC++
  Created by Indrajeet Roy
*********************************************************************/


#include<conio.h>
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

void main()
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);
	//Print the Menu:
	printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
	printf("\nEnter '4' to exit");
	scanf("%d",&ch);
	
	
	//Actual Processing:
	do
	{
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}
		
	}while(ch!=4);
}
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	fclose(fw);
}

void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(fw==NULL)
		abort();
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	fclose(fw);
}

void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
		abort();
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	fclose(fw);
}

The problems are
1)When the code runs,the write() does not pass control back after I press the *.
2)In the read(),it displays an unending loop of printing "3" for some reason!!

Please help.
Thanks
Indrajeet Roy

Hi Indrajeet,

The big mistake you did is you are using the print Menu statement outside the do..while loop. since you used outside do...while loop the ch value is always 1 and it will execute the case:1.So it falls in infinite loop. And use getchar() function instead of getche().
Since you use getche() in read function it prints only the last line entered but the file contains all the character you entered.

The below code will help you to execute with no bug.

#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

//Protoype of the functions
 
int read(char *fn);
int write(char *fn);
int append(char *fn);
 
void main()
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn); 
 
	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:
				write(fn);
				puts("File successfully written");
				break;
			case 2:
				read(fn);
				puts("File successfully read");
				break;
			case 3:
				append(fn);
				puts("File successfully appended");
				break;
			default: 
				printf("U Entered %d as wrong Option !!!!!!!!!! ",ch);
				puts("TRY AGAIN ONCE!!!!!!");
		}
 
	}while(ch!=4);
}
int read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(NULL == fw)
	{
		puts("File not found");
		return 1;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	fclose(fw);
}
 
int write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(NULL == fw)
	{
		puts("File not found");
		return 1;
	}
	printf("\nEnter the text.Press 0 to exit\n");
	while((ch=getchar())!='*')
		fputc(ch,fw);
	fclose(fw);
}
 
int append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(NULL == fw)
	{
		puts("File not found");
		return 1;
	}
	printf("\nEnter the text.Press * to exit\n");
	while('*'!=(ch=getchar()))
		fputc(ch,fw);
	fclose(fw);
}

Try this one also...

#include<conio.h>   // try to avoid conio.h
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);



	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}

	}while(ch!=4);
  return 0;
}
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}

Try this one also...

#include<conio.h>   // try to avoid conio.h
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main
{
    char fn[20];
    int ch;
    printf("\t\tEnter the name of the file\n\t\t");
    scanf("%s",&fn);



    //Actual Processing:
    do
    {
        //Print the Menu:
        printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
        printf("\nEnter '4' to exit");
        scanf("%d",&ch);
        switch (ch)
        {
            case 1:write(fn);
                break;
            case 2:read(fn);
                break;
            case 3:append(fn);
                break;
            default: printf("Please try again");
        }

    }while(ch!=4);
  return 0;
}
void read(char *fn)
{
    FILE *fw;
    char ch;
    printf("\n");
    fw = fopen(fn,"r");
    if(fw==NULL)
    {
     printf("\n Error: File Not Found!");
     return;
    }
    do
    {
        ch=fgetc(fw);
        printf("%c",ch);
    } while(ch!=EOF);
    printf("\n\n File Successfully read!");
    fclose(fw);
}

vo

Hi,

Use code tag to post your code.so it will be easy to point out the error with the line number

Thanks,
Perry

Try this one also...

#include<conio.h>   // try to avoid conio.h
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main
{
    char fn[20];
    int ch;
    printf("\t\tEnter the name of the file\n\t\t");
    scanf("%s",&fn);



    //Actual Processing:
    do
    {
        //Print the Menu:
        printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
        printf("\nEnter '4' to exit");
        scanf("%d",&ch);
        switch (ch)
        {
            case 1:write(fn);
                break;
            case 2:read(fn);
                break;
            case 3:append(fn);
                break;
            default: printf("Please try again");
        }

    }while(ch!=4);
  return 0;
}
void read(char *fn)
{
    FILE *fw;
    char ch;
    printf("\n");
    fw = fopen(fn,"r");
    if(fw==NULL)
    {
     printf("\n Error: File Not Found!");
     return;
    }
    do
    {
        ch=fgetc(fw);
        printf("%c",ch);
    } while(ch!=EOF);
    printf("\n\n File Successfully read!");
    fclose(fw);
}

vo

Hi vinitmitta,

The code is incomplete.

Hi vinitmitta,

The code is incomplete.

ya, thanks buddy .. i also noticed.. actually i posted complete code but i think there was some problem.. well here is the code

#include<conio.h> // avoid using this header file
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main function
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);



	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}

	}while(ch!=4);
 return 0;
}

void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}

void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(fw==NULL)
	{
	  printf("\n Error: Can't create new File!");
	  return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully written!");
	fclose(fw);
}

void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
	{
	 printf("\n Error: Can't open File!");
	 return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully appended!");
	fclose(fw);
}

ya, thanks buddy .. i also noticed.. actually i posted complete code but i think there was some problem.. well here is the code

#include<conio.h> // avoid using this header file
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions

void read(char *fn);
void write(char *fn);
void append(char *fn);

int main() // don't use void with main function
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);



	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}

	}while(ch!=4);
 return 0;
}

void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}

void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"w");
	if(fw==NULL)
	{
	  printf("\n Error: Can't create new File!");
	  return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully written!");
	fclose(fw);
}

void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
	{
	 printf("\n Error: Can't open File!");
	 return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getche())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully appended!");
	fclose(fw);
}

Hi vinitmittal,

Have u noticed one thing while reading the file.The read function prints only the last string that u entered. Because the file mode that you are using is wrong.

When you open a file, you must specify how it is to be opened. This means whether to create it from new, overwrite it and whether it's text or binary, read or write and if you want to append to it. This is done using one or more file mode specifiers which are single letters "r", "b", "w", "a" and + (in combination with the other letters). "r" - Opens the file for reading. This fails if the file does not exist or cannot be found. "w" - Opens the file as an empty file for writing. If the file exists, its contents are destroyed. "a" - Opens the file for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; this creates the file first if it doesn't exist. Adding + to the file mode creates three new modes:
"r+" Opens the file for both reading and writing. (The file must exist.) "w+" Opens the file as an empty file for both reading and writing. If the file exists, its contents are destroyed.
"a+" Opens the file for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.

Hi vinitmittal,

Have u noticed one thing while reading the file.The read function prints only the last string that u entered. Because the file mode that you are using is wrong.

When you open a file, you must specify how it is to be opened. This means whether to create it from new, overwrite it and whether it's text or binary, read or write and if you want to append to it. This is done using one or more file mode specifiers which are single letters "r", "b", "w", "a" and + (in combination with the other letters). "r" - Opens the file for reading. This fails if the file does not exist or cannot be found. "w" - Opens the file as an empty file for writing. If the file exists, its contents are destroyed. "a" - Opens the file for writing at the end of the file (appending) without removing the EOF marker before writing new data to the file; this creates the file first if it doesn't exist. Adding + to the file mode creates three new modes:
"r+" Opens the file for both reading and writing. (The file must exist.) "w+" Opens the file as an empty file for both reading and writing. If the file exists, its contents are destroyed.
"a+" Opens the file for reading and appending; the appending operation includes the removal of the EOF marker before new data is written to the file and the EOF marker is restored after writing is complete; creates the file first if it doesn't exist.

First thing this is'nt my program... i just made little change to remove logical error asked... i dint compile it...

okk... i compiled it now... one more error is there .. use of getche().. use getchar() instead of getche() function.. both in write and append functions...

okk... i compiled it now... one more error is there .. use of getche().. use getchar() instead of getche() function.. both in write and append functions...

Hi initmittal,

I told already use getchar() instead of getche(). Now it works fine when use getchar() function instead of getche().

Here is the code

#include<conio.h> // avoid using this header file
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions
 
void read(char *fn);
void write(char *fn);
void append(char *fn);
 
int main() // don't use void with main function
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);
 
 
 
	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}
 
	}while(ch!=4);
 return 0;
}
 
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}
 
void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"r+");
	if(fw==NULL)
	{
	  printf("\n Error: Can't create new File!");
	  return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getchar())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully written!");
	fclose(fw);
}
 
void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
	{
	 printf("\n Error: Can't open File!");
	 return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getchar())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully appended!");
	fclose(fw);
}

okk... i compiled it now... one more error is there .. use of getche().. use getchar() instead of getche() function.. both in write and append functions...

Hi Vinitmittal,

I already suggest use getchar() instead of getche(),

Here is the modified code,

#include<conio.h> // avoid using this header file
#include<stdio.h>
#include<stdlib.h>
//Protoype of the functions
 
void read(char *fn);
void write(char *fn);
void append(char *fn);
 
int main() // don't use void with main function
{
	char fn[20];
	int ch;
	printf("\t\tEnter the name of the file\n\t\t");
	scanf("%s",&fn);
 
 
 
	//Actual Processing:
	do
	{
		//Print the Menu:
		printf("\n1)Write\n2)Read\n3)Add to a prev file\n");
		printf("\nEnter '4' to exit");
		scanf("%d",&ch);
		switch (ch)
		{
			case 1:write(fn);
				break;
			case 2:read(fn);
				break;
			case 3:append(fn);
				break;
			default: printf("Please try again");
		}
 
	}while(ch!=4);
 return 0;
}
 
void read(char *fn)
{
	FILE *fw;
	char ch;
	printf("\n");
	fw = fopen(fn,"r");
	if(fw==NULL)
	{
	 printf("\n Error: File Not Found!");
	 return;
	}
	do
	{
		ch=fgetc(fw);
		printf("%c",ch);
	} while(ch!=EOF);
	printf("\n\n File Successfully read!");
	fclose(fw);
}
 
void write(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"r+");
	if(fw==NULL)
	{
	  printf("\n Error: Can't create new File!");
	  return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getchar())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully written!");
	fclose(fw);
}
 
void append(char *fn)
{
	FILE *fw;
	char ch;
	fw=fopen(fn,"a+");
	if(fw==NULL)
	{
	 printf("\n Error: Can't open File!");
	 return;
	}
	printf("\nEnter the text.Press * to exit\n");
	while((ch=getchar())!='*')
		fputc(ch,fw);
	printf("\n\n File successfully appended!");
	fclose(fw);
}

Ya, thanks buddy... i think you are confused with my name, its vinit mittal. Just call me vinit. :)

Ya, thanks buddy... i think you are confused with my name, its vinit mittal. Just call me vinit. :)

Hi Vinit,

I hope your problems get solved.

Thanks
Perry

Hi Vinit,

I hope your problems get solved.

Thanks
Perry

hey My Problem??

Its not my problem.!

hey My Problem??

Its not my problem.!

Ok vinit.

Hey thanks guys....I'll try the new and improved code now!!!!!

Some more questions:

1)How do I clear the screen in VC++ 2010 Express? It says that the clrscr() function does not exist in <conio.h>.

2)What's the difference b\w using getche() and getchar()?

3) Someone mentioned trying to avoid using <conio.h> May I know why?

thanks

Hey thanks guys....I'll try the new and improved code now!!!!!

Some more questions:

1)How do I clear the screen in VC++ 2010 Express? It says that the clrscr() function does not exist in <conio.h>.

2)What's the difference b\w using getche() and getchar()?

3) Someone mentioned trying to avoid using <conio.h> May I know why?

thanks

  1. The difference between getch(), getche(), getchar() is,

    getchar()
    This is a standard function that gets a character from the stdin. It works differently from others two. Whenever you are pressing any key then the these are kept in Buffer. After hitting enter the first character gets processed. And it obviously echoes on the screen.

    getch()
    This is a nonstandard function that gets a character from keyboard, does not echo to screen.It reads a character and never wait for Enter key.Just gets processed after getting any key pressed.And it never echoes(prints) the character on screen which u pressed.

    getche()
    This is a nonstandard function that gets a character from the keyboard, echoes to screen.
    it works same as getch() but it echoes(prints) on screen.

    Use getchar if you want it to work on all compilers. Use getch or getche on a system that supports it when you want keyboard input without pressing [Enter].

    And note that the return value of all three is int! You need this to properly check for EOF.

  1. If u want to clear the screen, use system function such as cls as below in visual studio
    #include <stdlib.h>
    
    void main()
    {
       system("cls");
    }
  • Since clrscr() function is not available in visual studio. For every function there should be prototype for particular function. But there is no prototype for clrscr() function in conio.h
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.