I have to make a program that manages info it obtains from a file, but I'm using Turbo C 3.0 which is ancient, so I'm getting errors trying to write to the file, here is my code:
#include <conio.h>
#include <stdio.h>
void main(){
clrscr();
int c;
FILE *datos;
datos = fopen("datos.txt","w");
c = fgetc (datos);
printf("%d",c);
fclose(datos);
getch();
}
Whenever I print it I get -1 as return. I know this must be something really simple but I'm having issues.
Check to make sure you have a valid file:
// use "r" for reading, "w" for writing
datos = fopen("datos.txt", "r");
if (datos)
{
int ch = fgetc(datos);
if (ch != EOF)
printf("%d\n", c);
else
printf("End of file!\n");
}
else
{
printf("Failed to open datos.txt for reading.\n");
}
You open the file in write only mode ("w"), but you are trying to read the file with fgetc.
Either open the file in read-only mode ("r"), then read the data, close the file, and open it again in write only mode to write it. Or open the file in a mode that support both reading and writing - check the documentation of Turbo-C for more info.
Try opening the file for "read" instead:
#include <conio.h>
#include <stdio.h>
#include <errno.h>
int main(){
int c;
FILE *datos;
clrscr();
datos = fopen("datos.txt","r");
if (!datos) {
printf ("Open failed, errno=%d\n", errno);
return 1;
}
c = fgetc (datos);
printf("%d",c);
fclose(datos);
getch();
return 0;
}
If it still fails, tell us the error#.
'include statements
main(){
char we;
char intt;
ifstream q("xyz.txt");
for (we=0;we<100;we++){
q >> intt;
printf("%c",q);
}
getch();
}
Related
Iam trying to print the content of a text file called text.txt in the terminal in c I have the following code but it doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
char str[255];
FILE *fp;
fp = fopen("text.txt","r");
while (fgets(str,255,fp)!=NULL){
printf("%s",str);
fclose(fp);
};
}
I couldn't find a solution please help
First of all, you are closing the file inside the loop. fclose should lie at the end of your program. Secondly, fopen() might fail (you don't have permission to read the file for example). So, don't forget to handle that too.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main(){
const int sz = 255;
char str[sz];
FILE *fp;
fp = fopen("input.txt","r");
if(fp == NULL){
// opening the file failed ... handle it
}
while (fgets(str,sz,fp)!=NULL){
printf("%s",str);
};
fclose(fp);
}
Here is another similar way
if (fp!=NULL)
{
// file open succeded. do sth with it.
fclose (fp);
}
Hope this helps and keep coding!
I am trying read a text from file in C but I am getting nothing in command prompt.Here my code is,please help me ...
#include <stdio.h>
#include <stdlib.h>
int main(void) {
FILE *file=NULL;
file = fopen("C:\\Users\\ylmzt_000\\Desktop\\Yeni klasör\\deneme.txt", "r");
if(file != NULL)
{
printf("----------------\n");
printf("content\n");
printf("-----------------\n");
int ch;
while((ch=fgetc(file)) != EOF)
{
putchar(ch);
}
printf("\n");
fclose(file);
}
return 0;
}
If fopen() returns NULL, an error occured. Construct an else part which may contain
fprintf(stderr, "cannot open '%s' (%s)\n", fn, strerror(errno));
where fn contains your filename.
Also see http://linux.die.net/man/3/fopen for further hints.
There is no obvious problem in your code. The problem may be related to the fact that the filename contains non-ASCII characters:
C:\\Users\\ylmzt_000\\Desktop\\Yeni klasör\\deneme.txt
Try renaming the directory with only ASCII characters.
I am trying to read a file character by character and print it on screen.
However, the character is not displaying, I am getting a box with 0001 in it.
This is my code
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "rb");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 1;
}
you need to check the return values from fopen, to ensure you opened the file successfully, you could be executing from the wrong directory.
Plus if your file is a TEXT file, you should be opening using "rt".
Basic File Opening modes in C is
"r"-reading
"w"-writing
"a"-append
"r+"-reading+writing
"w+"-reading+writing
"a+"-"reading+appending"
This code is enough to read .txt files
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *fp;
int ch;
fp=fopen("myfile.txt", "r");
while((ch = getc(fp)) !=EOF){
putc(ch, stdout);
}
fclose(fp);
return 0;
}
I've never had so much trouble writing data to files! I'm running GCC from MinGW, because I'm used to using GCC in Linux. I usually use the Linux system calls open(), write(), and read(), but I'm writing a Windows program now and I had trouble using read()/write() in Windows, so I'm just using the standard libraries. Anyway, the problem I'm having is I have no idea how to write to a file! I've defined "FILE *" variables, used fopen(), with "r+b", "wb", and "w+b", but I still cannot write to my output file with fwrite() or fprintf(). I don't know what I'm even doing wrong! Here's my source:
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#define DEBUG 1
/*** Global functions ***/
double highfreq(double deg);
/*** Global variables ***/
double sin_now;
unsigned int *ptr;
unsigned char *key, *infilename, *outfilename;
FILE *infile, *outfile, *keyfile;
const char *pipe_name="[pipe]";
int main(int argc, char *argv[]) {
unsigned int x, y, z;
if(argc!=3) {
fprintf(stderr, "Syntax error: %s <infile.txt> <outfile.wav>", argv[0]);
return 1;
}
if(argv[1][0]=='-') {
infile=stdin;
infilename=(unsigned char *)pipe_name;
}
else {
infilename=argv[1];
if((infile=fopen(infilename, "rb"))==NULL) {
fprintf(stderr, "Could not open input file for modulation.\n", infile);
return 2;
}
}
if(argv[2][0]=='-') {
outfile=stdout;
outfilename=(unsigned char *)pipe_name;
}
else {
outfilename=argv[2];
if((infile=fopen(outfilename, "wb"))==NULL) {
fprintf(stderr, "Could not open/create output file for modulation.\n", outfile);
return 3;
}
}
if(DEBUG) printf("Input file:\t%s\nOutput file:\t%s\n", infilename, outfilename);
fprintf(outfile, "Why won't this work!?\n");
fclose(infile);
fclose(outfile);
return 0;
}
double highfreq(double deg) {
double conv, rad;
conv=M_PI/180;
rad=deg*conv;
return sin(rad);
}
I'm eventually going to make a WAV file as output, hence the "highfreq()" function, but for now I can't even get it to write to a file! fprintf() returns with an error value of -1, if that helps anyone. I don't really understand, though because from what I read, this simply indicates there was an error, but nothing more.
outfilename=argv[2];
if((infile=fopen(outfilename, "wb"))==NULL) {
That's the second time in your code you assign the result of fopen to infile. You probably wanted outfile there.
I wrote the following C program to write data into a file.The program got compiled properly but nothing is getting written in the file.Please suggest modifications if required.
#include <stdio.h>
#include <errno.h>
int main()
{
int i;
FILE *fopen(),*fp;
fp = fopen("D:\Satish_SharedSubstance\V13.4-CT_Testing\LONGRUN_Testing\writetest.txt","w");
/*Create a file and add text*/
if(fp!=NULL)
{
fprintf(fp,"GRP \n");
fprintf(fp,"groupname group_1 \n");
fprintf(fp,"groupcomment group_1\n");
fprintf(fp,"jobnet 255 \n");
fprintf(fp,";\n");
for (i=1;i<=255;i++)
{
fprintf(fp,"GNT \n");
fprintf(fp,"jobnetname jobnet_t%d\n",i);
fprintf(fp,"jobnetnumber %d\n",i);
fprintf(fp,";");
}
/*writes data to the file*/
fclose(fp); /*done!*/
}
else
{
printf("Error opening file\n");
}
return 0;
}
Two things:
Get rid of the *fopen() in the variable declaration.
Backslashes must be escaped in C strings. Replace each '\' with a '\\'.
fp = fopen("D:\Satish_SharedSubstance\V13.4-CT_Testing\LONGRUN_Testing\writetest.txt","w");
Try replacing "\" with "\\" in Path.
You can do the mentioned below:-
FILE *fp = fopen("D:\\Satish_SharedSubstance\\V13.4-CT_Testing\\LONGRUN_Testing\\writetest.txt","w");