0

I'm having a problem converting a line of characters into an int array in C. I'm reading lines from a file and one of the lines is just numbers separated by some commas and spaces. I have a loop checking all of the characters and if its a number it puts it in a separate array if not it just continues. However, the output does not seem correct at all. Below is my code.

#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 200
#define SIZE 10
#define FILENAME "file.txt"


char *trimwhitespace(char *str)
{
  char *end;

  // Trim leading space
  while(isspace(*str)) str++;

  if(*str == 0)  // All spaces?
    return str;

  // Trim trailing space
  end = str + strlen(str) - 1;
  while(end > str && isspace(*end)) end--;

  // Write new null terminator
  *(end+1) = 0;

  return str;
}

int main(void)
{
    //disable output buffering on standard output.
    setbuf( stdout, NULL );

    int array [SIZE];
    FILE  *dataFile;
    char buffer[BUFSIZE];
    int lineNo = 0;

    int x= 0;

    char gc[BUFSIZE];

    //open the file
    puts("opening file");
    dataFile = fopen( FILENAME, "r" );
    if( dataFile == NULL )
    {
        fprintf( stderr, "file not found " );
        return 1;
    }

    puts("reading file");

    while( !feof( dataFile ) )
    {

        fgets( buffer, BUFSIZE, dataFile );
        ++lineNo;

        strcpy( gc,trimwhitespace( buffer ) );
        int y = 0;
        for( int i = 0; i < BUFSIZE; i++ )
        {
            if( isdigit( gc[i] ) )
            {
                int array[y];
                printf("%d ", array[y]);
                y++;
            }
            else if( gc[i] == " " || gc[i] == NULL )
            {
                continue;
            }
        }
        printf("\n");
    }

    fclose( dataFile );

    return 0;
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 1
    See [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – John Bollinger Sep 23 '16 at 20:37
  • You never do anything that converts characters from the input to numbers. In particular, the "`int array[y]`" in your inner loop has no such effect. – John Bollinger Sep 23 '16 at 20:43
  • What is `printf("%d ", array[y])` supposed to do? You just created the array on the previous line, but never initialized it. You're also accessing outside the array, since the maximum index of an array declared `int array[y]` is `y-1`. – Barmar Sep 23 '16 at 20:43
  • You can use `atoi()` to convert a string to an integer. – Barmar Sep 23 '16 at 20:44
  • "if its a number it puts it in a separate array". There's no code that does this. – Barmar Sep 23 '16 at 20:45
  • And you can use `strtok()` to break up a line into substrings. – John Bollinger Sep 23 '16 at 20:45
  • Your loop should stop when it reaches the end of the string. – Barmar Sep 23 '16 at 20:45
  • Putting `continue` at the bottom of a loop is useless. `continue` means to go to the next iteration of the loop, but that's what it does when it gets to the bottom of the loop body. – Barmar Sep 23 '16 at 20:46

0 Answers0