Note that there are some explanatory texts on larger screens.

plurals
  1. POregarding file i/o in c++
    text
    copied!<p>I have a part of a code that does the following: It reads in sentences from a file in a particular format, puts them in a vector. To probe whether the strings in the vector are stored correctly, I put debugging cout statements. I found that the last string member member of the vector is "". Why is this so? The file I am reading from ends with the last floating point value (that is stored in weight in each iteration). There is no whitespace or \n after that. I am pasting that part of the code in the form of a separate program below.</p> <pre><code>#include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;string&gt; #include &lt;vector&gt; using namespace std; int dist=0; void stringtolower(char *s) { int i=0; char c; while(s[i]!='\0') { c=s[i]; c=tolower(c); s[i]=c; i++; } } void cleanup(char *s) { int i=0; dist=0; while(*(s+i)=='\r' || *(s+i)=='\n' || *(s+i)=='\t') { dist++; i++; } while(*(s+i)!='\0'){ /*if(*(s+i)=='"' || *(s+i)=='`' || *(s+i)=='\'' || *(s+i)=='.') *(s+i)=' ';*/ if(*(s+i)==':' || *(s+i)=='\t' || *(s+i)=='\n' || *(s+i)=='\r' || *(s+i)=='"' || *(s+i)=='`' ){ *(s+i)='\0'; break; } i++; } return; } int isinlist(vector&lt;string&gt; sents, char *s){ for(int i=0;i&lt;sents.size();i++){ if(!sents[i].compare(s)){ return 1; } } return 0; } int main() { char *s=NULL; FILE *fp; fp=fopen("1.txt","r"); size_t len=0; ssize_t read; vector&lt;string&gt; sents; float weight; while(!feof(fp)) { read=getdelim(&amp;s,&amp;len,':',fp); cleanup(s); s=s+dist; fscanf(fp,"%f",&amp;weight); if(isinlist(sents,s)){ continue; } stringtolower(s); string str(s); //sentences.push(str); // Push sentence into FIFO queue for later processing sents.push_back(str); } for(int i=0;i&lt;sents.size();i++) { cout&lt;&lt;sents[i]&lt;&lt;endl; } } </code></pre> <p>Thanks a lot for your help.</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