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 6 years ago.
Improve this question
I am coding C language in Visual Studio C++.
In the first calling of this function there is no problem. It makes all of process but in the second calling VS give "Windows has triggered a breakpoint" error.
However, I compile and run the code in Linux, there is no problem.
void printDataPagePersons(int pageNumber)
{
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
printf("page of city : %d\n",sizeof(*pageofCity));
FILE* fp;
fp=fopen("x.dat", "rb");
fseek(fp, PAGE_SIZE*pageNumber,SEEK_SET);
fread(pageofCity, PAGE_SIZE,1, fp);
fclose(fp);
//OTHER PRINTING PROCESSES...
}
Here you allocate a buffer:
Person* pageofCity = (Person*)malloc(sizeof(Person)* RECORD_COUNT);
This is the size of that buffer:
sizeof(Person)* RECORD_COUNT
You then read data from a file into that buffer at this line of code:
fread(pageofCity, PAGE_SIZE,1, fp);
There you are telling the fread function to read a PAGE_SIZE of bytes into that buffer.
I'm guessing the cause of your problems is this condition is also true:
PAGE_SIZE > sizeof(Person)* RECORD_COUNT
Related
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 3 years ago.
Improve this question
I am doing a project in my class about file and i/o system calls. I have dumbed down the code below for the sake of my question. I have read the manual multiple times. I thought I understood read(), but clearly I'm missing something.
int a, bytesRead;
char buffer[150];
a = open("/home/JohnDoe/Dowloads/TestFile.dat", 0);
if (a < 0) {
printf("Error opening file TestFile.dat\n");
return 0;
}
bytesRead = read(a, buffer, 150);
printf("Bytes read: %d\n", bytesRead);
I'm lost to as why this read() call returns -1. If I understand the manual correctly, read() takes arguments file id, buffer and number of bytes to read. The integer, a (file id), was returned without error, I created my buffer and reading 150 bytes should be no problem because the file contains well over 150 bytes. Any help would be greatly appreciated.
Check errno. If read returns -1, the errno will be set and you can see, where the error happened.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 5 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.
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.
Improve this question
I keep running a memory leak when I print a bunch of times to a file by calling this function multiple times. There aren't any issues unless I actually print.
void print_fields_weights_energies(MKL_Complex16 *stored_fields,
MKL_Complex16 *stored_energies,
MKL_Complex16 *weights,
int steps,
int_st ist,
cns_st cns) {
/*Print The Fields Out To File*/
int walkers;
int fields;
int field_number;
double tau = steps*cns.dtau;
char message[150];
FILE *pf;
/*Name and Make File*/
sprintf(message, "fields-tau%f.dat", tau);
pf = fopen(message, "w+");
for (walkers=0; walkers<cns.max_number_walkers; walkers++) {
for (fields=0; fields<ist.n_spin_orbitals_sq; fields++) {
field_number = walkers*ist.n_spin_orbitals_sq*3+fields*3;
fprintf(pf, "%f\t %d\t %d\t", tau, walkers, fields);
fprintf(pf, "%f\t %f\t %f\t %f+%fi\t\t", stored_energies[walkers*3].real, stored_energies[walkers*3+1].real, stored_energies[walkers*3+2].real, weights[walkers].real, weights[walkers].imag);
fprintf(pf, "%f\t %f+%fi\t %f+%fi\n", stored_fields[field_number].real, stored_fields[field_number+1].real, stored_fields[field_number+1].imag, stored_fields[field_number+2].real, stored_fields[field_number+2].imag);
}
}
fflush(pf);
return;
}
You need an fclose() at the bottom of your function (and you don't need the fflush()--the file will get flushed when it gets closed).
You also should be checking for (pf == NULL) before referencing it, in case of an error on the fopen(). Getting an error message is far more useful than getting a SEGV.
For future memory leaks, I highly recommend the tool "valgrind"--it tends to point you to exactly where your memory leak occurs.
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 5 years ago.
Improve this question
here is the code given to me and a have also a text file where i need to get the Text from and compile the program.
You need to use open a file with fopen() first. But the current user needs to have perms to read/write the file.
We will use r to only read from a file. If the file is not read it will return NULL. You can fscanf() function to get the value of a file. The second parameter represents the type of the variable as in this case it's a string(char), third param is the mem address of the variable itself. Kind of like file version of scanf().
int main()
{
char a[1000];
FILE *myFile;
if ((myFile = fopen("C:\\myUSER\\newprogram.txt","r")) == NULL){
printf("Error! opening file");
exit(1);
}
fscanf(myFile ,"%s", &a);
printf("Value of a=%s", a);
fclose(myFile);
return 0;
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I was asked to solve a programming challenge, and there is this line which I don't understand can some one explain to me how can I pass the test cases to the program using this command, I think I have to store it in some file but I am not sure
size_t getline(char **lineptr,size_t *n, FILE *stream);
here is the full code
#include <stdio.h>
#include <stdlib.h>
size_t getline(char **lineptr,size_t *n, FILE *stream);
int main()
{
size_t maxLineLen=1024;
char *line = (char*)malloc(maxLineLen);
while(getline(&line, &maxLineLen,stdin)!= -1){
printf("Hello, World!\n");
printf("%s\n",line);
}
}
Seems you are asking how to run the given code and get input into.
getline(&line, &maxLineLen,stdin)
That reads a line from stdin. stdin is a standard file stream and is opened by the startup code for you. Without redirection, reading from stdin will get the input typed into the terminal
So to get input into the program you can do one of the following:
Run the program and then type each input line into the terminal.
Run the program and then redirect a file into the program. Example:
./my_program < my_input.txt
Are you given a file name?
Then the FILE * parameter would have to be opened via fopen.
See manual for fopen
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 3 years ago.
Improve this question
I'm trying to compile this program under Windows
(it's a program which turns bootable code in floppies. I got the source from here: http://www.acm.uiuc.edu/sigops/roll_your_own/1.bootstrap.html).
First I got the problem that it didn't read the INI file. That is solved now. Now I get a segfault on the following rule:
while(data < end)
I added output to the app, so it should be clear where the executing stops: no "."'s are printed out. I'm on Win64, with the Cygwin toolchain.
Thanks!
Yvan
A mistake is:
size = (int *)lSize;
Better:
*size = lSize;
and the function as:
void *loadfile(char *file, long *size)
...
and in calling context eg:
long size=0;
char *buffer = loadfile("blah.txt",&size);
if( buffer )
{
printf("\nstrlen = %lu, fsize = &ld", strlen(buffer), size );
}