I am trying to create a ppm file from inside a C program, but somehow it doesn't work.
It works fine when I am inside the IDE and run the program there. The program is executed and the file created inside the file's folder.
But once I build it and open the executable with a double click, the program runs in the terminal but does not create the file.
I am working on a mac and this is the relevant code, thanks in advance you all!
// from generalSettings.h
#define WIDTH 1100
#define HEIGHT 966
#define MYFILENAME "testimage.ppm"
int pictureArray[HEIGHT][WIDTH];
//from the main.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include "generalSettings.h"
int main()
{
triangle firstParentTriangle;
// these two functions "draw" sierpinsky triangles in the pictureArray
drawFirstParentTriangle(&firstParentTriangle);
drawChildTriangles(firstParentTriangle, numberOfRecursions);
create_ppm();
return 0;
}
void create_ppm()
{
unsigned char color_black[] = "000 000 000\n";
unsigned char color_white[] = "255 255 255\n";
FILE *p_file = fopen(MYFILENAME, "w");
if (NULL != p_file)
{
fprintf(p_file, "P3\n %d %d\n 255\n", WIDTH, HEIGHT);
for (int i = 0; i < HEIGHT; i++)
for (int j = 0; j < WIDTH; j++)
if (1 == pictureArray[i][j])
fprintf(p_file, color_black);
else
fprintf(p_file, color_white);
fclose(p_file);
}
}
Here, I have replaced the foreground and background variables with local ones.
Generally, the program inserts 1 to the pictureArray[][] on certain elements and leaves others with a 0.
For my problem, this should be the relevant code for a reproducable example.
EDIT: Problem solver. File was created in the user folder due to missing path.
this code is supposed to read a text file that contains integers, which then finds the squares of these numbers after putting them into an array.
After this its supposed to print the results onto a new text file "result.txt", but I keep getting the error "invalid operands to binary*(have 'int*' and 'int*')" on this line: square[x] = square[x] * square[x];
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main(){
int square[100][2];
int x;
int i;
FILE* input=fopen("in.csv","r");
FILE * f=fopen("result.txt","wb");
system("cls");
for(x=0;x<100;x++)
{
fscanf(input,"%d",&square[x][0]);
}
fclose(input);
for(x=0;x<100;x++)
{
square[x] = square[x] * square[x]; //this line produces the error
}
for(i=0;i<100;i++)
{
fprintf(f,"%d -> %d || ",square[i][0],square[i][1]);
}
fclose(f);
getchar();
}
I am using Eclipse IDE and MinGW-w64
I have tried finding solutions online but am stuck, any help or replies would be appreciated, thanks!
square[x] yields an array with two elements. You should reference the [0] and [1] elements like you do in the rest of your code.
square[x][1] = square[x][0] * square[x][0];
summary : system("clear"); isn't working well.
I'm using gcc, ubuntu 18.04 LTS version for c programming.
what I intended was "read each words and print from two text files. After finish read file, delay 3 seconds and erase terminal"
so I was make two text files, and using system("clear"); to erase terminal.
here is whole code.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
I'm very sorry, files and variables names are changed because of policy. Also, file contents also can not upload.
I can't find which part makes break Procedure-oriented programming. I think that was compiler error, because using one file read and system(clear); works well.
I also make two point variables, such as 'FILE *f1; FILE *f2; f1=fopen(file1); f2=fopen(file2)...`, but same result occur.
Is it compiler error? If it is, what should I do for fix these problem? Thanks.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
void printFiles(char *file1,char *file2,char *change1, char *change2){
FILE *f;
char *text = malloc(sizeof(char)*100);
f=fopen(file1,"r");
system("clear");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
//fscanf(f,"%s", text);
printf("%s ",text);
fflush(stdout);
//usleep(20000);
}
//sleep(3);
fclose(f);
printf("\n");
//all comment problems are appear here. and if I give delay, such as usleep() or sleep, delay also appear here. Not appear each wrote part.
f=fopen(file2,"r");
//while(!feof(f)){
while(EOF!=fscanf(f,"%s",text)){
if(strcmp(text,"**,")==0){
strcpy(text,change1);
strcat(text,",");
}
else if(strcmp(text,"**")==0){
strcpy(text,change1);
}
else if(strcmp(text,"##.")==0){
strcpy(text,change2);
strcat(text,".");
}
else if(strcmp(text,"##,")==0){
strcpy(text,change2);
strcat(text,",");
}
printf("%s ",text);
fflush(stdout);// The answer.
//usleep(200000);
}
fclose(f);
free(text);
sleep(3); //here is problem. This part works in the above commented part "//all comment problems are appear here."
system("clear"); //here is problem. This part works in the above commented part "//all comment problems are appear here."
}
int main(){
char file1[100] = "./file1.txt";
char file2[100] = "./file2.txt";
char change1[100]="text1";
char change2[100]="text2";
printFiles(file1,file2,change1,change2);
return 0;
}
Hint for
That's probably just buffering. Do fflush(stdout); before you sleep. – melpomene
Thanks.
You can try this solution for delay.
#include <time.h>
#include <stdio.h>
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(¤t);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(3);
printf("...oh man, waiting for so long...\n");
return 0;
}
Following solution is pretty quite the same of previous one but with a clear terminal solution.
#include <time.h>
#include <stdio.h>
#ifdef _WIN32
#define CLEAR_SCREEN system ("cls");
#else
#define CLEAR_SCREEN puts("\x1b[H\x1b[2J");
#endif
void delay(double seconds)
{
const time_t start = time(NULL);
time_t current;
do
{
time(¤t);
} while(difftime(current, start) < seconds);
}
int main(void)
{
printf("Just waiting...\n");
delay(2); //seconds
printf("...oh man, waiting for so long...\n");
delay(1);
CLEAR_SCREEN
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#include "bcio2.h"
int error, x;
char totalimpulse[80], averageimpulse[80];
void validate_number();
int main(void)
{
clrscr();
do{
printf("\nTotal Impulse delivered: ");
gets(totalimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
{ //error C2447
clrscr();
do{
printf("\nAverage Impulse delivered: ");
gets(averageimpulse);
validate_number();
} while (error != 0);
printf("You entry %d was valid\n", x);
getch();
return 0;
}
The brackets seen to match and there doesn't seem to be any unnecessary semicolons. I'm assuming this is the correct way to display input/validation. It works fine when run with just the do…while(); loop for totalimpulse but when I copy/paste the exact same between another pair of { } I get just that C2447 error.
The code that starts where the error is is not inside main, or any other function for that matter. If you remove the braces on the error line and the one preceding it, then your second loop will also beside of main. If you want that section to be a different function, you have to include the header for that function. What you put at the top for validate_number is just a promise that you will define that function somewhere (although if you mean for that section at the bottom to be validate_number, I'm pretty sure you don't want it to be recursive).
Right now you just have a block of code, outside any function.
I'm assuming from the rest of your code, that this block of code is supposed to be the definition of void validate_number();, like this:
void validate_number()
{
clrscr();
do{
// ...
return 0;
}
Do note that a void function cannot return a value, so your return 0 should be removed.
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?
%s needs a char* 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 czas on a new line you can specify this in the fprintf() statement like so:
fprintf(file,"%d\n", czas);