I’m trying to make a stopwatch program, which will write czas to file.txt. I started learning C today, so please be lenient for me, if it’s a stupid question, but the compiler doesn’t throw out any errors, and NetBeans also doesn’t display any exclamation marks. There is my code:
#include <windows.h>
#define sleep(x) Sleep(1000 * x)
#include <stdio.h>
#include <stdlib.h>
int a = 0;
int czas = 0;
int main (void)
{
FILE *file;
while (a < 30) { /*repeats only 30 times*/
a = a + 1; /*increases the counter for while loop*/
file = fopen("file.txt","w"); /*opens file.txt for writing*/
fprintf(file,"%s", czas); /*writes czas to file.txt*/
fclose(file); /*closes file.txt to save*/
czas = czas + 1; /*increases czas for writing to file*/
}
return 0;
}
Could somebody help me?
%sneeds achar*as referring parameter.You want to write out an
int, which expects%d.For details on this please see
man fprintf.In case you like to have every new value of
czason a new line you can specify this in thefprintf()statement like so: