Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 603737
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:57:49+00:00 2026-05-13T16:57:49+00:00

I have a part of a code that does the following: It reads in

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T16:57:49+00:00Added an answer on May 13, 2026 at 4:57 pm

    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.

    FILE *fp = fopen(..., "r");
    assert(!feof(fp));  // guaranteed, even if the file is 0 length
    

    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:

    FILE *fp = fopen(..., "r");
    char buffer[SIZE];
    while (fgets(buffer, sizeof(buffer), fp) != NULL)
    {
        // got some data, do something with it.
    }
    
    // fgets returned NULL, now let's check if it was because
    // we got to the eof or had an error
    if (feof(fp))
        // got to the end
    else
        // got an error 
    

    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:

    1. It only returns indicator provided it hasn’t already read any data when it reaches EOF
    2. It always returns indicator when it reaches EOF.

    If the former, you want to structure your code like:

    while (getdelim(&s,&len,':',fp) != GET_DELIM_EOF_VALUE)
    

    If the latter, you will need something like:

    while ((getdelim(&s,&len,':',fp) != GET_DELIMI_EOF_VALUE) ||
           (len != 0))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a game code (from ioquake3 project) that compiles part of gameplay binaries
I have a part of a code that counts what is in the x,y
I have some code that at one part will get executed a lot, and
I have a class, which is part of a code library project that was
I have the below code that does not seem to work at all :(
I have been required to write a function that reads the BSDF data format
I have the following code which is the bottleneck in one part of my
I currently have an application that does the the following: S: Loads a view
I have the following XAML code as part of a custom control: <telerik:RadTreeView x:Name=treeModules>
I have the following C++ code snippet (the C++ part is the profiler class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.