-1

I want to input the following lines from file("input.txt") :

a 1,2,3,4,5,6
b 1,8

(i.e a character followed by a white-space and then an array separated by comma)

I have tried following code:

int main(int argc, char *argv[])
{
    std::vector<int> arr;
    FILE *file = fopen("input.txt","r");

    while(!feof(file))
    {
        for(int i = 0; i < arr.size(); i++)
        {
            fscanf(file,"%s %d,",str,&arr[i]);
        }
    }
 }

Let me know correct way to doing this, as it is showing garbage values

Mike
  • 4,041
  • 6
  • 20
  • 37
  • 4
    Why is this tagged C? – Jabberwocky Sep 07 '18 at 09:58
  • Btw `feof()` is not recommended as https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong – Achal Sep 07 '18 at 09:59
  • What is `str`? You don't check if `fopen` was successful. `arr.size()` is initially 0, so your `for` loop never executes. Please read this: [mcve]. – Jabberwocky Sep 07 '18 at 10:00
  • You will want to look at [**Why is while ( !feof (file) ) always wrong?**](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – David C. Rankin Sep 07 '18 at 16:37

2 Answers2

0

First of all Manisha, you are witnessing unusual code behavior because the while loop, which you have used, never stops. Let me tell you why in a very simple way. The stopping condition you have specified in the while loop, i.e, feof() indicates if one has tried to read past the end of file. But you can never read PAST the end of the file, which means the while loop would never stop.

Find an alternative way to read through the file. There are lots of other ways, one of which I have shown below:

while (fgets(line, sizeof(line), file)) {
/* note that fgets doesn't strip the terminating \n(new line character) */
...
}
if (ferror(file)) {
    /* IO failure */
} else if (feof(file)) {
    /* format error (not possible with fgets, but would be with fscanf) or end of file */
} else {
    /* format error (not possible with fgets, but would be with fscanf) */
}
Harshith Rai
  • 3,018
  • 7
  • 22
  • 35
0

Is this supposed to be C or C++? You're using a C++ data type (std::vector), but C I/O routines. You haven't specified a type for str, either.

Assuming you mean to use C I/O routines, you could do something like this:

char str[SOME_LENGTH+1]; // where SOME_LENGTH is how big you expect the string to be

/**
 * Get the string at the beginning of the line; scanf should return 1
 * on a successful read.
 */
while ( scanf( "%s", str ) == 1 ) 
{
  size_t i = 0;
  /**
   * Read the sequence of integers and commas.  We consume the character
   * immediately following the integer, but don't assign it (the %*c
   * specifier).  So we'll consume the comma or newline following
   * the integer.  Since a letter isn't part of a decimal integer,
   * we'll stop scanning at the beginning of the next line.
   */
  while( scanf( "%d%*c", &arr[i++] ) == 1 )
    ; // empty loop
}

Note: this assumes your input is well-behaved, and that there are no spaces between the number and following comma (i.e., you don't have something like 1, 2 , 3).

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • while( scanf( "%d%*c", &arr[i++] ) == 1 ) Isn't this line signifies that you're taking an integer first followed by character ? – Manisha Keim Sep 15 '18 at 21:07
  • @ManishaKeim: Yes, it reads an integer followed by a character, but the `*` in `%*c` tells `scanf` that we’re not storing that character value anywhere. Without it, the `’,’` character would not be removed from the input stream, fouling up the next read. – John Bode Sep 16 '18 at 00:03
  • but if you see in the question, I want input as character followed by comma separated array. – Manisha Keim Sep 16 '18 at 07:29
  • @ManishaKeim: That’s what the `scanf` in the `while` loop condition is for. It reads the the beginning of the line as a string, then reads the sequence of numbers afterwards. – John Bode Sep 16 '18 at 23:43