Hello to all you guru's! I'm a newbie at C/C++. I'm trying to write a function that will
1) read input from a text file to a data array
2) perform comparison of new data and insert into the data array
3) write the data array into an output text file.
From the searches in this forum I noticed a lot of you saying to go the STDIO.H way and not IOSTREAM. Well I've written a function that compiles with 0 errors and 0 warnings but will not insert dName into the array. dName on the output file is for some reason limited to 4 characters. If you can, compile the code below and you'll see what I mean when you look at the output file. I think it might be a problem with strcpy? at the output if I add a
it will show only "masC"
I have absolutely no idea why this isnt working.
What I have:
If you've tried and cant figure it out also, I have to ask how would YOU write this function if you were given the above conditions?
p/s You will see the input file created if you compile this and run it the first time.
oh and btw, debugging via output file is hard. How do you keep track of values in variables if you cant use printf or cout to screen? Is there a way to monitor it?
Thanks in advance!
masCh
1) read input from a text file to a data array
2) perform comparison of new data and insert into the data array
3) write the data array into an output text file.
From the searches in this forum I noticed a lot of you saying to go the STDIO.H way and not IOSTREAM. Well I've written a function that compiles with 0 errors and 0 warnings but will not insert dName into the array. dName on the output file is for some reason limited to 4 characters. If you can, compile the code below and you'll see what I mean when you look at the output file. I think it might be a problem with strcpy? at the output if I add a
- Code:
fprintf(stream,"%s\n",dName);
it will show only "masC"
I have absolutely no idea why this isnt working.
What I have:
- Code:
#include "stdio.h"
#include "stdlib.h"
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPTSTR IpCmdLine, int nCmdShow)
{
FILE *stream;
//this is data to be inserted
double dTime=54.3; //can be changed to whateva
char dName[100] = "masCh man"; //can be changed to whateva
struct klas {
char nama[100]; //for those long names
double marks;
} skor[10];
if ((stream=fopen("skorlist.txt","r"))==NULL) { //if not found then
stream = fopen("skorlist.txt","w"); //create new file
for (int i=0;i<10;i++) {
fprintf(stream,"%s","Unnamed"+i+"\n");
fprintf(stream,"%i",(10-i)*10);
fprintf(stream,"%s","\n");
}
if( fclose(stream)) {
//data not closed
}
else {
fclose(stream);
}
//open file for readin
stream = fopen("skorlist.txt","r");
fseek(stream,OL,SEEK_SET);
char buffer[100], *stopbuffer;
for (int i=0;i<10;i++) {
fgets(skor[i].nama,100,stream);
fgets(buffer,100,stream);
skor[i].marks=strtod(buffer, &stopbuffer);//convert str to double
}
fclose(stream);
//insert new score
for (int k=0;k<10;k++) {
if dTime>skor[k].marks) {
for (int t=10;t>l;t--) {
skor[t].marks=skor[t-1].marks;
strcpy(skor[t].nama,skor[t-1].nama);
}
skor[k].marks=dTime;
strcpy(skor[k].nama,dName); //i think problem is here
dTime=0;
}
}
//write new scoring file
stream = fopen("newskor.txt","w");
for (i=0;i<10;i++) {
fprintf(stream,"%s",skor[i].nama);
fprintf(stream,"%-3.1f",skor[i].marks);
fprintf(stream,"%s","\n");
}
fclose(stream);
return 0;
}
If you've tried and cant figure it out also, I have to ask how would YOU write this function if you were given the above conditions?
p/s You will see the input file created if you compile this and run it the first time.
oh and btw, debugging via output file is hard. How do you keep track of values in variables if you cant use printf or cout to screen? Is there a way to monitor it?
Thanks in advance!
masCh