Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I am writing C-program that in the end will contain two large 2-dimensional arrays that I need to export to two csv.files.
The arrays (doubles) are 10000 rows long, and between 8 and 30 columns long, depending on what the user define, example below:
int NOS = 10000;
int Tau_length = 15;
Energy_system_Array[NOS][Tau_length];
I have created two file pointers:
FILE *fp1, *fp2;
At the end of the program, I have written these lines:
int array_rowit, array_colit;
fp1 = fopen("Energyarray.csv", "w");//create a file
if (fp1 == NULL)
{
printf("Error while opening the file.\n");
return 0;
}
for (array_colit = 0; array_colit<Tau_length; array_colit++){
for (array_rowit = 0; array_rowit< start_measure; array_rowit++){
fprintf(fp1, Energy_system_array[array_rowit,array_colit]);
fclose(fp1);
}
}
But when I compile the c-code, this is the message I get:
main.c: In function 'main':
main.c:413:22: error: incompatible type for argument 2 of 'fprintf'
fprintf(fp1, Energy_system_array[array_rowit,array_colit]);
^~~~~~~~~~~~~~~~~~~
In file included from main.c:1:0:
C:/MinGW/x86_64-w64-mingw32/include/stdio.h:378:15: note: expected 'const char * restrict' but argument is of type 'double'
int __cdecl fprintf(FILE * __restrict__ _File,const char * __restrict__ _Format,...);
^~~~~~~
I am a real novice when it comes to C-programing, so I hope someone can help me with this.I have searched around for different guides, but I haven't found any that describes my situation exactly.
I should mention that the csv files should be overwritten for each run of the program. It cannot save the values from an earlier run of the program.
To write the whole array as CSV, use:
for (array_colit = 0; array_colit<Tau_length; array_colit++){
for (array_rowit = 0; array_rowit< start_measure; array_rowit++){
fprintf(fp1, "%lf%s",Energy_system_array[array_rowit,array_colit],
(array_rowit<start_measure-1?",":""));
}
fprintf(fp1,"\n");
}
fclose(fp1);
Notes:
append a comma after each value but the last one
add a newline after every row
close the file only once everything has been written out.
Related
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I don't know why cannot compile simple project below by dev.
error: Project1.exe has stopped working.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
int n,d;
while(1){
printf("enter number");
scanf("%d",n);
d=n%10;
while(d!=0){
n=n/10;
printf("%d",d);
d=n%10;
}
}
return 0;
}
For one, your project does compile, since you get a run time error.
The run time error occurs because you are not using scanf correctly. The arguments to scanf after the format string should be pointers to the variables.
I don't know which compiler you are using, but any fairly modern compiler would have given you compiler warnings about this, e.g. here's Clang's output:
apa.c:9:20: warning: format specifies type 'int *' but the argument has type 'int' [-Wformat]
scanf("%d",n);
~~ ^
Changing this into scanf("%d",&n); makes your program work.
https://linux.die.net/man/3/scanf
First, the error that you got is not a compile, it's a run time error.
Second, it's happen when the run arrive to the line of scanf. This function required the variavbles address to put the value in.
Replace this:
scanf("%d", n);
With this:
scanf("%d", &n);
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a section of code that fails when I try to use fclose to close an output file. The code looks as follows:
void WriteArrayForCheck(int numLines, double **Array) {
char outFile2[300];
sprintf(outFile2, "OutputArray.txt");
FILE *outputFile2;
outputFile2 = fopen(outFile2, "w");
int incRow;
for (incRow = 0; incRow < numLines - 1; incRow++) {
fprintf(outputFile2, "%lf,%lf,%lf\n",
Array[incRow][1], Array[incRow][2], Array[incRow][3]);
}
fclose(outputFile2);
}
I end up with an error related to my executable that says:
free(): invalid next size (normal): 0x0000000001ce1710 ***
and a whole bunch of other stuff that doesn't make sense... The ironic thing is if I comment out the line related to fclose, then the program does not crash and runs perfectly fine... I have not had this issue before. I am sure that my matrices are incremented properly as well. PLease let me know what you think.
The code as posted does not seem to have an obvious problem.
Note however that:
You should always check the return value of fopen() and report failure instead of risking undefined behavior by passing NULL to fprintf and/or fclose.
You should simplify the first few statements as FILE *outputFile2 = fopen("OutputArray.txt", "w");
If you do something more complicated to compute the filename, post it as the problem may lie in seemingly harmless code that you omitted.
The fprintf conversion specifier for double is %f, the l modifier is useless and ignored in this context. It is required in fscanf() to distinguish between float and double, but not needed in fprintf because float values are always passed as double to variable argument functions.
The index values 1, 2 and 3 might be incorrect, arrays are 0 based in C.
Stopping the loop at incRow < numLines - 1 is unexpected too, why omit the last row of the matrix? If you really mean it, a comment would be helpful to clarify why.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Improve this question
For some reason anything I try results in numbers after the decimal place being disregarded.
sscanf(argv[5], "%.2lf", &add4); doesn't work
add4 = atof(argv[5]); doesn't work
Any help?
1: program name (aka for the program im trying to solve currently, i need the following parameters. of these, double isnt getting saved properly)
2: string
3: int
4: int
5: double
all but the double are being copied into my arrays fine. I've tried the above two ways of copying the double but both result in numbers after the decimal being .00
Declarations:
char add1[50];
int add2, add3;
double add4;
This call adds a new line to a list .csv file:
inv add banana 2 2 2.50
where below, command has already been assigned the string "add".
what would end up listed would be:
"banana, 2, 2, 2.00" at the end of my text file
if(strcmp(command,"add") == 0)
{
strcpy(add1, argv[2]);
add2 = atoi(argv[3]);
add3 = atoi(argv[4]);
add4 = atof(argv[5]);
FILE *fp3 = fopen("replica.csv", "w");
for(j=0;j<i;j++)
{
fprintf (fp3, "%s,%d,%d,%.2lf\n", item[j], quantity[j], limit[j], cost[j]);
}
fprintf(fp3, "%s,%d,%d,%.2lf\n", add1, add2, add3, add4);
fclose(fp1);
fclose(fp3);
remove("inventory.csv");
rename("replica.csv", "inventory.csv");
}
Enable more warnings and check the return value of sscanf.
% clang test.c
test.c:5:28: warning: invalid conversion specifier '.' [-Wformat-invalid-specifier]
int n = sscanf(argv[1], "%.2lf", &x);
~^
1 warning generated.
atof works for me. Make sure you're including stdlib. You could also try stdtod(argv[5], NULL).
Try:
sscanf(argv[5], "%lf", &add4);
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
What i have here is a struct that i want to print to a file. The structure consists of a series of singel character ints where pek3 Points at the first object containing a number in the structure.
fprintf didnt work and this just gives me the error:
missing ')' Before '->'
FILE *filen;
int h;
talstrul *tepek = pek3;
filen = fopen("summadata.txt","w");
for(h=1; h<=maxlen; h++)
{ int fput(tepek->num,filen);
tepek = tepek->next;
}
fclose(filen);
Your example is incomplete - so we have to guess.
f = fopen("summadata.txt","w");
for(int h=1; h<=maxlen; h++) {
fprintf(f, "%d\n", tepek->num);
tepek = tepek->next;
}
fclose(f);
should work.
fprintf works as follows:
the first argument is the file handle, that is what you get from fopen.
the format string, here "%d\n", describes, what you want to print. Here it is a integer ("%d") and then a newline ("\n").
then comes the arguement(s) to the formatstring. In this case the integer, I guess that is tepek->num.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am very new to C and having a problem here. I am attempting to pass a file numbers.in through the below script. numbers.in contains 2 lines as follow:
12,34,56789
123456,789,0123
I am attempting to recognize the comma delineation.
#include <stdio.h>
void main(int argc, char *argv[])
{
int p ,n ,x ; //Converted ints.
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 );
{
printf("got the sequence (%d,%d,%d)\n",x,p,n);
}
}
I am running the script like:
./a.out < numbers.in
Currently my script returns completely different numbers! What am I doing wrong here? Is the file sending them as characters so I need to somehow convert to ints? (I tried saving as chars and then later converting chars to ints and also got strange numbers - but different strange numbers!)
SOLVED, bad semicolon usage >_<
while ( fscanf(stdin,"%d,%d,%d\n",&p,&n,&x) == 3 ); <-- remove this semicolon