Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>long *readSeqFile(char *filename); </code></pre> <p>You might want to make <code>filename</code> const:</p> <pre><code>long *readFileName(const char *const filename); </code></pre> <p>UPDATE: while this answers the original question, it's not very good C practise. If, as the poster says, he wants to read a list of integers from a file and get it back as an array of long, then the prototype is probably going to look something like this:</p> <pre><code>int readSeqFile(const char *const filename,long **longArray,size_t *const len); </code></pre> <p>and the implementation (in sketch form):</p> <pre><code>#include &lt;stdio.h&gt; #include &lt;stdlib.h&gt; const size_t longArrayChunkSize=100; int readSeqFile(const char *const filename,long **longArray, size_t *const len) { FILE *fp; size_t nInts=0,curBufferLen; long tmp; fp=fopen(filename,"r"); if(fp==NULL) return -1; *longArray=malloc(longArrayChunkSize*sizeof(long)); if(*longArray==NULL) return -1; curBufferLen=longArrayChunkSize; // let's assume file is a list of integers, one per line while(read from file and !feof()) { (scan line into tmp) longArray[nInts++]=tmp; if(nInts==curBufferLen) (realloc *longArray by longArrayChunkSize, increase curBufferLen) } *len=nInts; fclose(fp); return 0; } </code></pre> <p>Call it like this: <code>result=readSeqFile(filename,&amp;longArray,&amp;arrayLen);</code> <code>longArray</code> is of type <code>long*</code> and it is the caller's responsibility to free it afterwards. The result is zero on success and -1 on error.</p> <p>This is where you win with C++ and STL classes like <code>vector&lt;long&gt;</code> since you can just <code>push_back()</code> each long as it comes out of the file without having to worry about memeory management.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload