file output
-
Please help me with a segment code which can redirect the output to a file instead of using >> in the commandline. I am using c language. oam
Try this:
FILE *file;
file = fopen("C:\\Progrem files\\test.txt","w");
CString output;
for(int i = 0 ; i < 10 ; i++)
{
output.Format("this is test string number %d",i);
fputs(output.GetBuffer(output.GetLenght() + 1),file);
}
fclose(file);now,if you will open the test.txt file in your C:\Program files - guess what???:laugh: Good luck, Eli
-
Try this:
FILE *file;
file = fopen("C:\\Progrem files\\test.txt","w");
CString output;
for(int i = 0 ; i < 10 ; i++)
{
output.Format("this is test string number %d",i);
fputs(output.GetBuffer(output.GetLenght() + 1),file);
}
fclose(file);now,if you will open the test.txt file in your C:\Program files - guess what???:laugh: Good luck, Eli
-
FILE *file;
E:\process.c(145) : error C2275: 'FILE' : illegal use of this type as an expression ;) oamHave you included
stdio.h
? However, if you are going to use MFC, I would construct aCFile
object instead.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
Have you included
stdio.h
? However, if you are going to use MFC, I would construct aCFile
object instead.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
yes i have included
#include stdio.h
Im using visual C. How can i construct a struct CFile? oam When opportunity knocks, open the door as it might comes once in a timempapeo wrote: How can i construct a struct CFile? I (erroneously) assumed you would be using MFC.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
FILE *file;
E:\process.c(145) : error C2275: 'FILE' : illegal use of this type as an expression ;) oamThis error may be incorrect! The code provided by eli5021979 is correct, accept that it is not competely C code. The CString is an C++ class, get rid of it. Use a char buffer in stead: char buffer[128] (whatever size you need). The error will probably disappear. Heck, in the example code, I would get rid of the temporary buffer and just use fprintf() instead. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
This error may be incorrect! The code provided by eli5021979 is correct, accept that it is not competely C code. The CString is an C++ class, get rid of it. Use a char buffer in stead: char buffer[128] (whatever size you need). The error will probably disappear. Heck, in the example code, I would get rid of the temporary buffer and just use fprintf() instead. INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
what is the loop for in the above code, i get a lot of errors. Pliz give the idea of how to write the segment code in C oam
mpapeo wrote: what is the loop for in the above code, It executes the two statements within the curly brackets 10 times. mpapeo wrote: i get a lot of errors. And those are?? mpapeo wrote: Pliz give the idea of how to write the segment code in C Hasn't that already been provided?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
-
what is the loop for in the above code, i get a lot of errors. Pliz give the idea of how to write the segment code in C oam
mpapeo wrote: what is the loop for in the above code For dimenstartion puposes only! It should produce a file containing the following: "this is test string number 0this is test string number 1this is test string number 2this is test string number 3this is test string number 4this is test string number 5this is test string number 6this is test string number 7this is test string number 8this is test string number 9" Note: All variables must be declared at the start of the scope in C programs. You can not do things like for(int i=0;i<10;++i).
int DoFileOutTest1()
{
int i;
char buffer[64];
FILE *file = fopen("test.txt","w"); // creates or recreates file
if( !file )
return 0; // failed to create file
for(i = 0 ; i < 10 ; ++i) {
sprintf(buffer,"this is test string number %d\n",i);
fputs(buffer,file);
}
fclose(file);
return 1;
}
// OR
int DoFileOutTest2()
{
int i;
FILE *file = fopen("test.txt","w"); // creates or recreates file
if( file )
return 0; // failed to create file
for(i = 0 ; i < 10 ; ++i) {
fprintf(file,,"this is test string number %d\n",i);
}
fclose(file);
return 1;
}INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
mpapeo wrote: what is the loop for in the above code For dimenstartion puposes only! It should produce a file containing the following: "this is test string number 0this is test string number 1this is test string number 2this is test string number 3this is test string number 4this is test string number 5this is test string number 6this is test string number 7this is test string number 8this is test string number 9" Note: All variables must be declared at the start of the scope in C programs. You can not do things like for(int i=0;i<10;++i).
int DoFileOutTest1()
{
int i;
char buffer[64];
FILE *file = fopen("test.txt","w"); // creates or recreates file
if( !file )
return 0; // failed to create file
for(i = 0 ; i < 10 ; ++i) {
sprintf(buffer,"this is test string number %d\n",i);
fputs(buffer,file);
}
fclose(file);
return 1;
}
// OR
int DoFileOutTest2()
{
int i;
FILE *file = fopen("test.txt","w"); // creates or recreates file
if( file )
return 0; // failed to create file
for(i = 0 ; i < 10 ; ++i) {
fprintf(file,,"this is test string number %d\n",i);
}
fclose(file);
return 1;
}INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
FILE *file = fopen("test.txt","w"); // creates or recreates file E:\process.c(147) : error C2275: 'FILE' : illegal use of this type as an expression
once i declared the FILE i get errors oamYou need to find a book or sight on C programming. Here: 1) Create and empty console project. 2) Create a new new file named simple.c. 3) Copy the following into simple.c.
// simiple.c
#include "stdio.h"
int main(int argc, char* argv[])
{
FILE* fp = fopen("test.txt","wt");
if( fp )
{
int i;
for( i=0; i<10; ++i )
fprintf(fp,"Line %d\n",i);
fclose(fp);
}
return 0;
}- Press F5. 5) Open the file test.txt in you editor to see results. That is all! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen
-
You need to find a book or sight on C programming. Here: 1) Create and empty console project. 2) Create a new new file named simple.c. 3) Copy the following into simple.c.
// simiple.c
#include "stdio.h"
int main(int argc, char* argv[])
{
FILE* fp = fopen("test.txt","wt");
if( fp )
{
int i;
for( i=0; i<10; ++i )
fprintf(fp,"Line %d\n",i);
fclose(fp);
}
return 0;
}- Press F5. 5) Open the file test.txt in you editor to see results. That is all! INTP "The more help VB provides VB programmers, the more miserable your life as a C++ programmer becomes." Andrew W. Troelsen