Hi. I need to make a program which outputs an array to a file. The output file needs to have two columns, one labeled m(e) another labeled v, but I can't get the columns of the array to be separated in the output file, it just prints a string of characters. Here's my current code:

for (k = 0; k < (16); k++)

{

fprintf(output, "%d %d\n", table[k][0], table[k][1]);

}

fclose(output);

Any tips?

I assume table is an array of integers? Yes?

If you want the numbers aligned up right then add width fields printf(output, "%10d %10d", ...) will print them right justified in a 10 character field. If you want them left justified then add - character printf(output, "%-10d %-10d", ...)

Thanks for that. One more thing. If I want to set the first values in the output table to characters, but as you correctly guessed table is an array of integers, how would I go about this? I am presuming that there is no way to include characters in an array of integers so should I create an array which is output to the file first containing the characters I want, then the array of integers is output to the file?

Just use two arrays and print both at the same time

char names[20][80]; // array of characters
int scores[20];

// put this line in a loop.  You can put as many arguments to printf() as you want.
fprintf("%-20s %-10d\n", names[i], scores[i];

Thanks. That is what I was considering doing. I am struggling with my array of characters though. It is causing a seg fault at file output point.
Here's the code:

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

int main ( int argc, char* argv[] )

{
	int i, j, k, **lattice = 0, row, col, sum = 0;
	int average, check, pcntlatt, table[16][2], startlim;
	char coltitle[2] = "v", v, m;
	
	FILE         *output;
	const char    out_fn[]="0604632_proj2.out";


	printf("Input size of square lattice: ");
	scanf("%d", &row);

	col = row;

	startlim = 2;
	
	lattice = malloc (row * sizeof(int *));
	srand ( (unsigned)time ( NULL ) );
	
	
	for(i = 0; i < row; i++)
	{
		lattice[i] = malloc(col * sizeof(int *));
	}
	
	
	if (lattice == NULL)
	{
		printf ("Out of Memory\n");
		fprintf(output, "Out of Memory\n");
		exit(0);
	}
	
	
	for(k = 0; k < (16); k++)
	{
		table[k][0] = k;
	}
	
	
	for(i = 0; i < row; i++)
	{
		for (j = 0; j < col; j++)
		{
			lattice[i][j] = rand() % startlim;
		}
	}

	printf ("1st test\n");
	
	pcntlatt =  (3*row*col)/5;
	
	/*coltitle[0] = m;
	coltitle[1] = v;*/
	
	table[0][1] = 0;
	
		
	while (table[0][1] < pcntlatt)
	{
		for (k = 0; k < 16; k++)
		{
			table[k][1] = 0;
		}
		
		
		i = rand() % row;
		j = rand() % col;

	
		if (lattice [i][j] != 0)
		{
	
			(lattice [i][j])--;

			i = rand() % row;
			j = rand() % col;

			(lattice [i][j])++;

		}

	for (i = 0; i < row; i++)
		for (j = 0; j < col; j++)
			if (lattice[i][j] >= 16)
				printf ("%d is above largest value allowed in table\n", lattice[i][j]);
			else
				table[lattice[i][j]][1]++;
	}

	
	output = fopen ("0604632_proj2.out", "w");
	
	/*printf("%s %s\n", coltitle[0], coltitle[1]);
	fprintf(output, "%-10s %-10s\n", coltitle[0], coltitle[1]);*/

printf ("2nd test\n");	

	for (k = 0; k < (16); k++)
	{
		printf("%s %s %d %d\n", coltitle[0], coltitle[1], table[k][0], table[k][1]);
		fprintf(output, "%-10d %-10d\n", table[k][0], table[k][1]);
	}
	fclose(output);
	
	
	free(lattice);
	lattice = NULL;
	
	printf ("All tests passed\n");
	
return (0);
}

I have tried a few different solutions and commented out the ones I didn't try last. Where am I going wrong?

>>printf("%s %s %d %d\n", coltitle[0], coltitle[1], table[k][0], table[k][1]);

coltitle is just an array of one character, not an array of strings. It was declared as char coltitle[2]; "%s" tells printf() that the next argument is a null-terminated string. But what you sent it was just a single character -- coltitle[0].

So if put

printf("%s %d %d\n", coltitle[2], table[k][0], table[k][1]);

This should print the string of characters?

Thanks for you help. I have figured out what I am doing wrong. For future reference though, is it possible to declare an array of strings? Isn't that effectively an array of character arrays?

Thanks for you help. I have figured out what I am doing wrong. For future reference though, is it possible to declare an array of strings? Isn't that effectively an array of character arrays?

You are exactly right . Array of strings is effectively an array of character arrays. As string is nothing but a character array.

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.