|
maibam debina wrote: utility.c is the program file where the function is declared What function? You know, functon declarations should be inside header files and you should include just header files (as rule of thumb for beginners, at least).
Quote: rom there I was aiming to call to main.c that y I include it You cannot call a file (main.c is a file).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
In Utility.c its consist of already one created header file Utility.h which consist of struct for waveread do i need to add on this and if needed which way i have to?
#define _USE_MATH_DEFINES
#define PI M_PI /* pi to machine precision, defined in math.h */
#define TWOPI (2.0*PI)
#ifndef UTILITY_H_
#define UTILITY_H_
struct WavHeader {
char chunkid[4];
int chunksize;
char format[4];
char subchunk1id[4];
int subchunk1size;
short int audioformat;
short int numchannels;
int samplerate;
int byterate;
short int blockalign;
short int bitspersample;
char subchunk2id[4];
int subchunk2size;
};
float * wavRead(char*, int*);
#endif /* UTILITY_H_ */
|
|
|
|
|
You should include Utility.h if you need either WavHeader or wavRead (or both).
Utility.c should contain the wavRead function definition (its implementation).
You should NOT include Utility.c in main.c .
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Is it always neccessary to create header file,if i want to call a function which is in another file?
|
|
|
|
|
It is the good practice (the bad practice is copying the function declarations in the source code that needs them).
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
It is almost always necessary, and there is no reason not to use header files. Therefore you should never include a .c file.
It all comes down to the question where the actual implementation of the function can be found within the final executable program. The compiler will read your .c files one by one and generate the machine code for all functions implemented therein. All include statements will be resolved before the compiler even sees those files, therefore including another .c file is equivalent to copying the entire code of that .c file! This may result in multiple copies of the same function, and, as a result, the linker may be unable to generate a program.
Technically you can avoid these issues without the help of header files: all you need are declarations of the data types and functions declared somewhere, that you need in your current .c file. The header files are used as a means to assemble this information in just one place, rather than copy the declarations to every .c file that needs them. This way you can ensure that all files are properly updated with the relevant information, whenever the data type or function declarations change!
GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)
|
|
|
|
|
hi,
Your code is working fine & generating random sequence of 65350 times.
|
|
|
|
|
ya it is actually i was planning to make entire program as function and create another main.c that display only the output of the function..
|
|
|
|
|
Hello
My name is Shuvo Biswas. I am new to programming . Please, help me to choose a IDE that is easy to use and best among other.
Thank you .
|
|
|
|
|
|
|
<blockquote class="quote"><div class="op">Quote:</div>
#include"Utility.h"
#include"Utility.c"
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define FALSE 0
#define TRUE !FALSE
float square(float b);
char pathname[MAXPATHLEN];
int main(int argc,int *argv[])
{
int count,i;
struct direct **files;
int file_select();
if (getwd(pathname) == NULL )
{
printf("Error getting path\n");
exit(0);
}
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{
printf("No files in this directory\n");
exit(0);
}
printf("\n Number of files = %d\n",count);
for (i=1;i<count+1;++i)
{
printf("\n%s\n ",files[i-1]->d_name);
srand(12345);
int i,j;
float y,sum=0.0;
float a=0.2;
float summation=0.0;
int SizeOfData;
float *data;
data=wavRead(files[i-1]->d_name,&SizeOfData) ;
printf("\n\n========================================================\n");
printf("Generated random number\t || Squaring of random number:\n");
printf("=========================================================\n");
for(i=0;i<SizeOfData;i++)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
printf("%f\t",f);
y=square(f);
printf("\t\t%f\n",y);
sum +=y;
}
printf("\n===================================================\n");
printf("\n TOTAL SUM OF THE SQUARED NUMBER := %f\t\n",sum);
summation=sum/SizeOfData;
printf("\n ENERGY OF NOISE SIGNAL := \t%f/%d=%f\n\n",sum,SizeOfData,summation);
FILE *fout;
fout=fopen("NOISE SIGNAL","w");
j=0;
sum=0.0;
srand(12345);
fprintf(fout,"\n================================================================\n");
fprintf(fout,"\nGENERATED RANDOM NUMBER || SQUARING OF GENERATED RANDOM NUMBER\n");
fprintf(fout,"\n=================================================================\n");
while(j<SizeOfData)
{
float f= ((float)rand()/(float)(RAND_MAX))*a;
y=square(f);
fprintf(fout,"\t%f\t\t\t%f\n",f,y);
sum +=y;
j++;
}
fprintf(fout,"\n===============================================\n");
fprintf(fout,"\nTOTAL SUM OF THE SQUARED NUMBER:=\t%f",sum);
summation=sum/SizeOfData;
fprintf(fout,"\nENERGY OF NOISE SIGNAL e(n):=\t%f/%d = %f\n\n",sum,SizeOfData,summation);
fclose(fout);
}
float square(float b)
{
float z;
z=b*b;
return(z);
}
</blockquote>
|
|
|
|
|
You could just convert this code into a function which you can then add to a different project.
|
|
|
|
|
can u help me little deeper how to convert it .?
|
|
|
|
|
What's the problem? Just change the file name to something else, and change "main" to the name of your new function, with the appropriate parameters and return type.
|
|
|
|
|
which procedure i have to run and do i need to create header file for the function?
|
|
|
|
|
You don't run anything. Just create a new source file, define your function, by name, parameters and return type. You then use that from another file, either by defining it in the source, or by providing a header file with the definition.
Something like ...
# include double myfunction(int count, double* numbers)
{
double result = 0.0;
return result;
}
int main()
{
double myset[] = { 1.0, 2.3, 4.4 };
extern double myfunction(int count, double* numbers);
double answer = myfunction(3, myset);
}
|
|
|
|
|
thanks as before Richard but in which way i have to run in terminal i m using ubuntu..
|
|
|
|
|
Use the editor (vi, emacs etc) to create your files and run your compiler or make processor to build them. If you really don't understand these basics of your system, then I suggest you get hold of, and study, a book or the Linux man pages.
|
|
|
|
|
Man, you are patient.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Only very occasionally. I'm doing penance for my dumb question in the Database forum yesterday.
|
|
|
|
|
In C++, you can only have one main.
The standard explicitly says in 3.6.1:
A program shall contain a global function called main,
hich is the designated start of the program.
[...] This function shall not be overloaded.
You can have two functions called main. The name is not special in any way and it's not reserved.
What's special is the function, and it happens to have that name. The function is global.
So if you write a main function in some other namespace, you will have a second main function.
namespace secondMain
{
int main() { return 0; }
}
int main()
{
secondMain::main();
}
The first main function is not special - notice how you have to return explicitly.
|
|
|
|
|
However, the OP code is C , you know, another programming language.
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|
|
Hello guys,
I have a problem. I'm writing to a log file, but when I do the application throws:
An unhandled exception of type 'System.AccessViolationException' occurred in ****
Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
The code (unmanaged) I'm using is this one:
inline bool writeLog(nsCLog::eSeverity severity, string message)
{
ofstream myfile;
if( ((int)severity) >= mLogLevel)
{
myfile.open(mStrSource.c_str(), ios::in | ios::out | ios::app);
if(!myfile.is_open())
{
throw "Unable to open the file: " + mStrSource;
}
myfile << "TESTE" << endl;
myfile.close();
}
}
return true;
};
When receiving this error, the code points to the _Lock() of file fstream:
virtual void __CLR_OR_THIS_CALL _Lock()
{ if (_Myfile)
_CSTD _lock_file(_Myfile);
}
Other info:
Developed under VS2010 (Incially the project was Winx32 but now it's Winx64) (.NET Framework 4).
The open() method of ofstream creates the file and the file is writable but throws the exception when try's to write on the file.
Thank you guys in advanced.
modified 1-Aug-14 12:23pm.
|
|
|
|
|
Just for curiosity: why are you throwing a string and catching a bad_alloc ?
THESE PEOPLE REALLY BOTHER ME!! How can they know what you should do without knowing what you want done?!?!
-- C++ FQA Lite
|
|
|
|