I'm having troubles with the following code

#include <stdio.h>
#include <ctype.h>

int main (void) {

	int i=0,j=0,row;

	char str1[5], str2[5];

	char machine[4][5]={0};

	FILE *fp;

	fp = fopen("text.txt","r");

	while (fscanf(fp,"%s %s",&str1,&str2) == 2 ) {
		row = str1[1];
		while (str2[i] != NULL) {
			machine[row][i]=str2[i];
			i++;
		}
	}

	for (i=0;i<5;i++)
		for (j=0;j<5;j++)
			printf("%s\n",machine[i][j]);


}

And i have a text files containing data in the following format

Z2 abcde
Z1 vwxyz
Z3 qrstu

What I'm trying to accomplish is to assign the characters into a character array into the form,

[v][w][x][y][z]
[a][c][d][e]
[q][r][s][t]

The above code showed that all are null rather than what it should be

Any insights?

Recommended Answers

All 2 Replies

Noted a slight error.

Since Z2 is being read in as a string, I tried doing the following in the attempt to extract the digit from the string but it failed :

row = atoi(str1[1]);

You can do most of the work in scanf(). For example:

fscanf(in, " %*[Zz]%d %[^\n]", &index, buf);
fgetc(in); /* Discard the newline (if present) */

It's not quite as good as a regular expression, but suffices for strict formats. There's also extra work interpreting the values, but all of this is straightforward once you have the index and the string:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    FILE *in = fopen("test.txt", "r");

    if (in != NULL) {
        char buf[BUFSIZ];
        char (*data)[6];  /* Assuming that data lines are 5 characters exactly */
        int rows = 0;

        /* Count the lines in our file */
        while (fgets(buf, sizeof buf, in) != NULL)
            ++rows;

        clearerr(in); /* Good idea before C99, now required */
        rewind(in);

        data = malloc(rows * sizeof *data);

        if (data != NULL) {
            int index;
            int i;

            /* Hedge against a malformed file */
            for (i = 0; i < rows; i++)
                data[i][0] = '\0';

            /* At last we can parse the file as desired */
            while (fscanf(in, " %*[Zz]%d %[^\n]", &index, buf) == 2) {
                fgetc(in); /* Discard the newline (if present) */

                /* Correct the 1-based indices in our file */
                if (index > 0 && index <= rows)
                    strcpy(data[index - 1], buf);
            }

            /* Print a report of the properly ordered lines */
            for (i = 0; i < rows; i++) {
                printf("%d: '%s'", i + 1, data[i]);
                
                if (data[i][0] != '\0')
                    puts("");
                else
                    puts(" Potentially malformed file detected");
            }

            free(data);
        }
    }

    return 0;
}

There's an implicit assumption that the file contains a permutation of the lines in the range of [1,N], so any blank lines in the final array suggest an improperly generated file. If that doesn't meet your needs, the changes are fairly trivial to allow bogus lines and simply ignore them.

Finally, my code is intended as a start in writing your own code. If this is homework and you turn in the above program, you'll probably be punished for cheating because there are subtle hints that it was written by someone with more experience in C. Just sayin', use it at your own risk. ;)

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.