|
I have a scaled down version that only reads and prints the data. This "mini" version does not produce the exception that the version in the large program does. I copied the mini version into the large program and the exception reappears. I have tried adding parts of the large program to the mini version to discover what part of the large program is causing the fscanf error, but I have not had any luck so far. Any ideas?
here is the mini version
#include <stdio.h>
const int yc = 10; // Just use some value
void test() {
int yinitial;
printf("starting npv in npvpixel");
FILE* agefile;
printf("agefile set");
int inita=0;
int inage;
int time1;
int zc=0;
printf("initialized in npvpixel");
time1=0;
float npv1a=0.0;
/*goes through all 20 age groups*/
/* for(zc=1; zc<21;zc++) { */
printf("opening file");
//if (zc==1){
/*opens file only when first age group is called*/
if ((agefile=fopen("c:\\stinit.txt","r"))==NULL){
printf("can not open initial file"); }
else {
printf("initial age file open");
/* Set pointer to beginning of file: */
fseek( agefile, 0L, SEEK_SET );
printf("start of file found");
/*allows access to all values in file*/
for (yinitial=0; yinitial
|
|
|
|
|
simaneal wrote:
I have tried adding parts of the large program to the mini version to discover what part of the large program is causing the fscanf error, but I have not had any luck so far. Any ideas?
Which implies that both the mini program and the large program are now identical. Yes? If not, then there are still parts of the large program yet to be added.
As a side note, when posting code snippets, surround them with
tags and use the Preview button. While the compiler does not mind, reading code that is all left-aligned is painful.
<hr><p align="right"><small>"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown</small></p>
|
|
|
|
|
The entire program including its two functions, both
of which use fscanf is over 2500 lines long. I have
tested parts separately in mini programs to make sure
they work and to check that they are doing what I want
correctly. Unfortunately not all the parts want to
work together.
What order would you suggest I add things to the mini
program since it is part of a function called by the
main program? Should I create a stripped down mini
main and build the function completely then build up
the main again?
I am at a loss as to why the mini would work, but not
when added to the rest of the program.
simaneal
|
|
|
|
|
simaneal wrote:
I am at a loss as to why the mini would work, but not
when added to the rest of the program.
This tells me that whatever code has not been added to the mini program is suspect.
You could also take the larger program and remove pieces of it until it starts working.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Playing around with the mini program some more I discovered that changing
fscanf( agefile,"%d", &inita );
to
fscanf( agefile,"%d", inita );
produces tha same erroras the full program.
Any ideas on what I should look for in the full program?
When is the & needed?
simaneal
|
|
|
|
|
simaneal wrote:
When is the & needed?
See here. Notice that the optional arguments (those after the format string) are locations, not values. That is why you must use the address-of (&) operator.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I have been adding to the mini program and discovered that the violation first shows up when I try to initialize a large array that has been dynamically allocated. All the dynamically allocated arrays, except the last 2 are intialized without a problem. I have checked the programming and it is the same for the last 2 arrays as for all the others (I copied and pasted then changed only the names). Could the access violation occur because of a lack of memory? If so how could I check?
If not any other ideas?
simaneal
|
|
|
|
|
simaneal wrote:
Could the access violation occur because of a lack of memory?
Doubtful, unless you are talking about several hundred MB. Most likely you are writing to an area that does not belong to your program. Sometimes the effect does not show up until further downstream. Show me the array(s) and how you are initializing them.
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Here is the code for allocating memory usinf malloc and for intializing the arrays The problems arrizes when I try initializing the last 2 arrays allocated.
//allocation of dynamic memory forfirst of 20 sets (60 arrays total)
int *pix1;
if ((pix1 = (int*)malloc(array_size * sizeof(pix1)))== NULL) {
(void)printf("ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
int *pixm1;
if ((pixm1 = (int*)malloc(array_size * sizeof(pixm1))) == NULL) {
(void)fprintf(stderr, "ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
int *pixhigh1;
if ((pixhigh1 = (int*)malloc(array_size * sizeof(pixhigh1))) == NULL) {
(void)fprintf(stderr, "ERROR: Malloc failed");
(void)exit(EXIT_FAILURE); /* or return EXIT_FAILURE; */
}
//initialization of 1 of 20 sets
for(y=0; y
|
|
|
|
|
What is the value of array_size , and why multiply it by 4?
In the for loop, what is the value of ycoor ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
The array_size is 117 (the number of elements in the array)
Is the 4 your talking about the size of an integer? Wouldn't you have to allocate enough memory for each element (117) at size int(4)?
Am I doing that wrong? If so how should it be done?
The ycoor value is 117, also the number of elements in the array.
Sorry about the code. Next time I will remember to tag it.
simaneal
|
|
|
|
|
simaneal wrote:
Is the 4 your talking about the size of an integer?
Yes, but you coded the size of a pointer. It just so happens that both are 4 bytes!
simaneal wrote:
Am I doing that wrong? If so how should it be done?
No, you're right, but it helps to be able to explain why you are doing something a certain way.
If you omitted this memory allocation and initialization code, would the problem (i.e., fscanf() crashing) persist?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
On the mini program if I eliminate the initialization the problem dissappears. I can not eliminate the initialization and allocation on the large program and still have it do what it was designed to do. I need the arrays to store my solution sets. If you have any other ways of storing and outputting large data sets of integers I would be happy to hear it.
simaneal
|
|
|
|
|
Since your code is only allocating about 1400 bytes from the heap, I doubt very seriously that's the problem. You might want to comment out just the initialization code and see what that does. If it starts working, that would indicate that the initialization code is writing past one of the three arrays, and messing with memory that fscanf() is later using. Are there any other places that write to pix1 , pixm1 , or pixhigh1 ?
Also, why are the printf() and exit() calls preceeded with void ?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
pix1, pixm1 and pixhigh1 are used in several other places. There values change based on several calculation results.
the printf and exit codes use voids because that is how I was shown to do it. Is there a better way? Is that wrong?
simaneal
|
|
|
|
|
Followig the local variables using breaks most of the arrays are initialized ok, but their values change (to cxx0030:Error:expression can not be evaluated)latter when they are not being called. I have also noticed that a variable used in a for loop changed when it should not have it went from 116 (which is what it should be) to 116547629.
Can you think of anything that could cause these changes?
simaneal
|
|
|
|
|
simaneal wrote:
I have also noticed that a variable used in a for loop changed...to 116547629.
Nothing special about that number.
simaneal wrote:
Can you think of anything that could cause these changes?
Yes, something like:
for (int x = 0; x < 117; x++)
{
int &r = x;
int *p = &x;
r = 116547629;
*p = 116547629;
}
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I am tring to fill the buffer with text then output that information to a text file. Then line
int nBytesToWrite = FillBuffer(1024);
Gives me an error "FillBuffer: identifier not found"
Am I missing a header? I am using C++ .net to compile.
|
|
|
|
|
Is FillBuffer() a method of BinaryReader ? If so, where's the object at?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
Here is the code for my prog.
#include <iostream>
#include <iomanip>
#include <fstream>
#include "afx.h"
using namespace std;
int main(int argc, char* argv[])
{
int nSizeLimit = 1000;
int nBytesWritten = nSizeLimit + 1;
CFile outFile;
while(int i=0<10000)
{
if(nBytesWritten > nSizeLimit)
{
CString strFileName;
strFileName.Format("LittleFile%d.txt", nBytesWritten++);
if(outFile.m_hFile != CFile::hFileNull)
outFile.Close();
outFile.Open(strFileName, CFile::modeWrite | CFile::shareDenyWrite);
nBytesWritten = 0;
}
char buf[512];
int nBytesToWrite = FillBuffer(1024);
if(nBytesToWrite == 0)
break;
outFile.Write(buf, nBytesToWrite);
nBytesWritten += nBytesToWrite;
}
return 0;
}
|
|
|
|
|
Andrew Admire wrote:
while(int i=0<10000)
What is the purpose of this? Initialize i and compare it to 10000 in two separate statements like:
int i = 0;
while (i < 10000)
... That aside, the variable i is never incremented.
Andrew Admire wrote:
outFile.Open(strFileName, CFile::modeWrite | CFile::shareDenyWrite);
What if the file does not already exist? Don't you want to create it, too?
Andrew Admire wrote:
char buf[512];
What does this variable do? It's not being assigned anything.
Andrew Admire wrote:
int nBytesToWrite = FillBuffer(1024);
I already asked about FillBuffer() . You are using it incorrectly. Why is it even there?
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
This is a sample code someone gave me. It should create multiple files. So, for the fillbuffer part... I have no idea. The char buf array, I think it holds the words before sending it to file. As for the file... yes if it does not exist I would like to create it.
|
|
|
|
|
Andrew Admire wrote:
The char buf array, I think it holds the words before sending it to file.
Where do these words come from?
Andrew Admire wrote:
As for the file... yes if it does not exist I would like to create it.
Then you'll also need to use CFile::modeCreate .
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|
|
I'm trying to create a LM database using text files.
|
|
|
|
|
I'm not familiar with an LM database. Is that:
London Medical
Literary Machine
List Manager
"Ideas are a dime a dozen. People who put them into action are priceless." - Unknown
|
|
|
|