Hi,

I have to use fscanf to read the numbers in a line. The line is in the file "FileName1" and may or may not have EOF.
I am using the following code and it is stuck in an infinite loop.

FILE *Fileget;
 vector<int> get_value;
 Fileget = fopen(FileName1, "r");
 char c;
 int f1;
 //while((c=getc(Fileget)) != '\n' && c != EOF)
 while(!feof(Fileget))
 {
        fscanf(Fileget,"%d",&f1);
        get_value.push_back(f1);
        f1 = 0;
  }
        fclose(Fileget);
        // Print the vector.
        for(int l=0; l<(int)get_value.size(); l++)
        {
                fprintf(stderr,"%d ",get_value[l]);
        }

The line which i am reading is : 1 -2 3 -4 -5 6 -7 -8 9 10 11 -12 -13 -14 -15 -16 17 -18 -19 20 21 -22 23 -24 -25 26 27 28 29 -30 31 32 33 -34 35 36 -37 38 39 -40 41 -42 -43 -44 45 46 -47 -48 -49 50 51 52 53 54 55 56 -57 -58 -59 60 61 62 -63 -64 -65 -66 67 68 69 -70 -71 72 73 -74 75 -76 -77 78 -79 80 81 82 83 84 -85 86 87 -88 -89 -90 -91 -92 93 -94 -95 -96 97 -98 -99 -100 -101 -102 -103 104 105 106 107 -108 -109 110 111 -112 -113 -114 -115 116 -117 -118 119 -120 -121 122 -123 -124 -125 -126 -127 128 129 130 131 132 133 -134 135 136 137 138 139 140 -141 -142 -143 -144 145 146 147 148 -149 150 151 -152 153 -154 -155 -156 -157 -158 -159 -160 161 162 -163 -164 165 166 167 168 -169 170 171 172 -173 174 175 176 177 -178 179 -180 -181 182 -183 -184 185 -186 -187 188 -189 -190 -191 -192 -193 -194 195 196 -197 -198 -199 200 201 202 -203 204 205 -206 -207 -208 209 -210 -211 -212 -213 -214 -215 216 217 -218 -219 -220 221 -222 -223 -224 -225 -226 -227 228 -229 -230 -231 -232 -233 -234 235 -236 237 -238 239 -240 -241 -242 243 -244 245 -246 -247 -248 -249 250 -251 -252 -253 -254 255 -256 -257 -258 -259 260 261 262 263 -264 -265 266 267 268 269 -270 -271 -272 273 -274 -275 -276 -277 278 -279 280 281 282 283 284 285 286 287 288 289 290 291 292 -293 -294 -295 -296 -297 -298 -299 -300 -301 302 303 -304 -305 -306 -307 -308 -309 -310 -311 -312 -313 -314 -315 316 317 -318 -319 -320 Any help is appreciated.

Member Avatar for iamthwee

Why use fscanf, this is c++?

feof is not a good idea either.

Hi,

Thanks for the help. i have one doubt... I am doing this now. I am not sure I should equate fscanf to "NULL" or "0". i believe that NULL stands for new line character.

FILE *Fileget;
        Fileget = fopen(FileName1, "r");
        char c;
        int f1;
        
        while(!feof(Fileget))
       {
               if(fscanf(Fileget,"%d",&f1) == NULL)
               break;
               get_value.push_back((f1));
               f1 = 0;
        }
        fclose(Fileget);

Why use fscanf, this is c++?

feof is not a good idea either.

I am using fscanf because i need numbers with positive and negative sign I only know getline which returns string in c++.

I am not sure how can i do in c++. I need the numbers so that i can compare then with numbers with negative and positive sign.

Any help is appreciated

Member Avatar for iamthwee

Well if it is just one line, can't you do?

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(void)
{
    //read in file
    
    ifstream read ("test.txt");
    
    int var;
    while ( read >> var )
    {
       cout << var <<endl;
    }
    
    read.close();
    cin.get();
}

Or are you saying you have many lines in the file and you only have to store the numbers on the first line?

Or something entirely different.

Thanks...No there is only one. could u please tell me what is the purpose of :

"cin.get()"

Member Avatar for iamthwee

cin.get()

Just waits for a key press- such as press enter to terminate program. If you're running your program from the command prompt you wouldn't need it. Some IDEs pause the program for viewing for you. Others don't.

Thanks for the help...

Hi,

i wish to know when i do the following code how it is taking care of "EOF" and "\n".

#include <iostream>
#include <fstream>
#include <vector>

using namespace std;

int main(void)
{
    //read in file
    
    ifstream read ("test.txt");
    
    int var;
    while ( read >> var )
    {
       cout << var <<endl;
    }
    
    read.close();
    cin.get();
}

Thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.