PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Help! in/out files - stuck

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
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
Perhaps you'd like to give us code that isn't riddled with missing brackets,extra brackets, typos (OL rather than 0L), etc?
While I'm at it, you have a serious problem here (and most likely your "problem with strcpy"):

Code:

struct klas {
char nama[100]; //for those long names
double marks;
} skor[10];
...
{code}
...
for (int t=10;t>l;t--) {
skor[t].marks=skor[t-1].marks;


You're assigning to skor[10] when it doesn't exist, trampling memory - it could be dname that gets trampled and broken (and thus the strcpy will stop when it hits the ascii 0 or whatever crap is being stuffed into it.
masChman · 31 Oct 2002, 10:25 UTC — doh
doh!

Heheh actually the program is at my home computer. I transferred it via text file to my ipaq and brought it to work so I can type in to post on the net. I dont have net at home you see =( That is why I missed out on brackets and some typos.

You might be right with the t=10 bit, it should be t=9 eh? I didnt notice that. It always needs a third person to fix oversight such as that.

I will fix it and try it at home in a sec. Finish work in 20 mins wuuhoo. I will let you know if that fixes it tomorrow morning (you americans dont sleep yet ya hear!) hehe

Thanks!
BurningSheep · 31 Oct 2002, 10:29 UTC
Code:

fprintf(stream,"%s","Unnamed"+i+"\n");
fprintf(stream,"%i",(10-i)*10);
fprintf(stream,"%s","\n");

you should look up fprintf and formatting strings in general, they are not supposed to be used like this.

Code:

fprintf(stream, "Unnamed %i\n", i);
fprintf(stream,"%i\n",(10-i)*10);


And instead of outputting debug info to files, you can just use the debugger and put watches on vars you're interested in.

Your code is very hard to read, don't be afraid to use spaces and tabs ;)
masChman · 01 Nov 2002, 02:06 UTC — oh
Oh thanks! You've both helped me fix the problem. It was exactly the t=10 that caused the strcpy hitting the /o char or whatever that tells it to end the string. Trampling memory - I have to remember that.

Wow, and to think I went to read about strcpy, strcat, and for (int i=0;i<100;i++) skor.name[i]=dname[i] etc and still it did not function.. I should have come by here earlier. =)

About the suggestion for using debug.. Please explain or point me to a guide that can teach me how to monitor variables using it. I've tried compiling for x86 debug and to be honest I am at a loss to how its supposed to function. I looked through the editor and couldnt find any reference to monitoring a variable.

Thanks!
You start a debug session in VC++ / eVC++. You can hit F5 to start a debug session or use the toolbar button. You can set a breakpoint to stop execution at a certain point, then view / set variable values in the View window, etc.

Dan East