I’m writing some strings to a file using the following function…
void writeText(const char* desc){
FILE * pFile;
pFile = fopen ("CycleTestInfo.txt","a+");
fputs (desc,pFile);
fclose(pFile);
}
…inside of a for loop:
for(int i=0; i<numCycles; i++){
string cycle("---NEW CYCLE ");
cycle+=(char)i;
cycle+= "---\r\n";
writeText(cycle.c_str());
}
I have two issues though.i doesnt show up in my textfile and the newline does not appear for the first string written in my text file. For example, if numCycles is 4, I get the following output in my textfile.
---NEW CYCLE Cycle Done!
---NEW CYCLE ---
Cycle Done!
---NEW CYCLE ---
Cycle Done!
---NEW CYCLE ---
Cycle Done!
When I want it to look like this:
---NEW CYCLE 1---
Cycle Done!
....
idoesnt show up in my textfileIt’s because you are writing character with ASCII value
1. Value of character'1'is different and can be easily retrieved by adding value of'0'toilike this:char c = '0' + i;the newline does not appear for the first string written in my text file
First time
iis equal to0which is also the value of the terminating character'\0'Check out this article: C++ Character Constants