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.
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
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<string> sents, char *s){
for(int i=0;i<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<string> sents;
float weight;
while(!feof(fp))
{
read=getdelim(&s,&len,':',fp);
cleanup(s);
s=s+dist;
fscanf(fp,"%f",&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<sents.size();i++)
{
cout<<sents[i]<<endl;
}
}
Thanks a lot for your help.
Because you are not handling end of file (eof) correctly.
You can only tell that you’ve reached the eof when you’ve tried to read beyond the end of the file. Consider the case of a 0 length file. When that happens, this will be the case.
That is, even though there is no more data, feof will not return true until it actually tries to read the next byte.
What you need to do is detect end of file during reading. For example:
If getdelim is written properly, it should return an indicator when it has reached end-of-file. There are two different ways it may be written:
If the former, you want to structure your code like:
If the latter, you will need something like: