hello....i am trying to read a some integers with spaces in a file into an array, say the integers in a text file are in the shown way

1 2 3 5
13 34 2
122 2 1 34
34 23 343 53

so now i wish to read them into an integer array and print them line by line as

{1,2,3,5}
{13,34,2}
{122,2,1,34}
{34,23,343,53}

i am trying to use the code

int temp, line[128], i, j;
FILE *fp = fopen("abcd.txt","r");
	
if ( fp != NULL )
{
    i=0;
    while(!feof(fp))
    {
        fscanf(fp, "%d", &temp);
        if(temp!='\n')
        {
            if(temp!=' ')
            {
                line[i]=temp;
            }
            i++;
        }
        else
        {
            line[i]='\0';
            for(j=0;j=i;j++)
            {
	
                printf("%d",line[j]);
				
                line[j]='\0';
            }
            printf("\n");
            i=0;
        }
    }
}

but this isn't giving me the required result....so lemme know what corrections have to be made

fscanf(fp, "%d", &temp);
if(temp!='\n')

This is an impossible case unless one of the numbers happens to match the numeric value of '\n', and if that happens you'll get unpredictable behavior. You told scanf() to read integers, and it will discard all whitespace in the process.

What you need to do is read the file line by line, then split up each line into fields for your array:

#include <stdio.h>

int main(void)
{
    FILE *in = fopen("test.txt", "r");
    
    if (in != NULL) {
        char line[BUFSIZ];
        
        while (fgets(line, sizeof line, in) != NULL) {
            char *start = line;
            int field;
            int n;
            
            while (sscanf(start, "%d%n", &field, &n) == 1) {
                printf("%d ", field);
                start += n;
            }
                
            puts("");
        }
        
        fclose(in);
    }
    
    return 0;
}

Be mindful of the sscanf() loop in that example, because it uses a somewhat tricky way of stepping through the string. If you don't save the number of characters that were extracted and move forward accordingly, the loop will be infinite. For example, this is the wrong way:

while (sscanf(line, "%d", &field) == 1) {
    printf("%d ", field);
}

sscanf() isn't smart enough to use anything but the string you give it as the source, so this loop will continually extract the first field in line . Contrast this with something like std::istringstream in C++, where the buffer is updated internally to reflect extractions.

An alternative is to avoid sscanf() entirely and do the conversions yourself. This is more of a manual parse:

char *start = line;
char *end;

while ((start = strpbrk(start, "0123456789")) != NULL) {
    printf("%d ", strtol(start, &end, 0));
    start = end;
}

But the process is ultimately the same:

  1. Find the next integer
  2. Convert and print
  3. Find the next non-integer
  4. Repeat at 1 until an integer cannot be found
commented: well said +3
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.