Hello guys! i really need your help.

In my program, I am reading the contents of the file.

File looks like this:

1 ART ACE APE
2 BAT BOY
3 CAT COP CUP CAP CUT

I want to store the words in an array. one array per line.

I just would like to ask how do you separate the number from the words when reading from the file? And how will the computer know that it is already going to a new line, thus must fill the next array. Knowing when the next line starts troubles me because each line could have any number of words, max of 8 words per line.

I have been thinking of these for the past three days,

and so far this is what I got,
i know this is short but with this code, I already get a segmentation fault. i don't know why.

Please, help me.

int main() {

	FILE *ifp, *ofp;

	ofp = fopen("input.txt", "a");

	ifp = fopen("output.txt", "r");

	char data[8][8][3];
	int a,b,z;
	for (a=0; a<20; a++)
		for (b=0;b<20; b++)
			for (z=0; z<3; z++)
				data[a][b][z] = ' '; 

	char number[20];



	char c;

	while ((c = getc(ifp)) != EOF) {
	printf(c); // i did this to check if it is successful reading
	}
	
	
}

please shed some light. thank you very much.

Recommended Answers

All 11 Replies

So you are looping your output.txt with ifp looks illogical naming. Why 'a' mode for ofp? (which is never used)
Why lines 11 to 14 and at least why a<20 and b<20 and line 9 says char data[8][8][3]?

Hello guys! i really need your help.

In my program, I am reading the contents of the file.

File looks like this:

1 ART ACE APE
2 BAT BOY
3 CAT COP CUP CAP CUT

I want to store the words in an array. one array per line.

I just would like to ask how do you separate the number from the words when reading from the file? And how will the computer know that it is already going to a new line, thus must fill the next array. Knowing when the next line starts troubles me because each line could have any number of words, max of 8 words per line.

I have been thinking of these for the past three days,

and so far this is what I got,
i know this is short but with this code, I already get a segmentation fault. i don't know why.

Please, help me.

int main() {

	FILE *ifp, *ofp;

	ofp = fopen("input.txt", "a");

	ifp = fopen("output.txt", "r");

	char data[8][8][3];
	int a,b,z;
	for (a=0; a<20; a++)
		for (b=0;b<20; b++)
			for (z=0; z<3; z++)
				data[a][b][z] = ' '; 

	char number[20];



	char c;

	while ((c = getc(ifp)) != EOF) {
	printf(c); // i did this to check if it is successful reading
	}
	
	
}

please shed some light. thank you very much.

Generally speaking (since I don't know the specifics of what you're trying to do), you want to have one word per line, in a two dimension array.

The key is the separator between the words (a space), and the newline separates the last word in the line, and the line itself, (both).

A file for reading input would never be set to append mode "a". There's nothing at that end of the file to be read! Just open it for reading, instead.

If you REALLY want to read in three words at a time, and put them into one row of a 2D char array, then you can easily do it by using fgets().

It can happen that some words will not be full strings in C, because they just don't have an end of string marker right after their last letter: '\0'. Even though you can't see this char, it's important for working with strings, as strings. (that is, using the string functions).

So you are looping your output.txt with ifp looks illogical naming. Why 'a' mode for ofp? (which is never used)
Why lines 11 to 14 and at least why a<20 and b<20 and line 9 says char data[8][8][3]?

Hi PyTony! :) Thank you for that! I made a for ofp because we need to produce an output file that would contain the compared words.

Generally speaking (since I don't know the specifics of what you're trying to do), you want to have one word per line, in a two dimension array.

The key is the separator between the words (a space), and the newline separates the last word in the line, and the line itself, (both).

A file for reading input would never be set to append mode "a". There's nothing at that end of the file to be read! Just open it for reading, instead.

If you REALLY want to read in three words at a time, and put them into one row of a 2D char array, then you can easily do it by using fgets().

It can happen that some words will not be full strings in C, because they just don't have an end of string marker right after their last letter: '\0'. Even though you can't see this char, it's important for working with strings, as strings. (that is, using the string functions).

Hello adak! i actually want to do it this way:
array[line_number][word_number] = word

I was able to do that already, but i did it this way:
array[line_number][word_number] = ith letter of the word
i did this to be able to determine if a new word is encountered.
but what happened was:
array[line_number][word_number][20] = 'a' //or some other letter, though my limit for that counter is 3, typing in 20 would give letters :( which I find impossible because I have a line that goes like this:

if(counter == 3) {
counter = 0;
word_number += 1;
}

what i want to do next after having that array, is to compare the different lines if they have the same word. then I will produce lists of line numbers that don't have the same word. :)

You can do it that way, but the last index number will always refer to a letter in a word.

In a virtual way, you've reduced the list of words to a list of a list, of words. So array[row][wordNum] is what you will use with strcmp(), to see if the word is a repeat or not.

if((strcmp(array[row][0], array[row][1])) == 0) {
  //it's a matching name
}

Put the above inside a loop, where row and word numbers are variables.

For strcmp() to work, you need two things:

1) to include string.h into the program, and

2) every word must be a true string, ending with an end of string char, which it appears you have.

Thank you Adak! I will try to do that :)

array[row][0]

But the problem is that this array[row][0] doesn't just contain the word CAT but it contains lots of random characters :( Even though I restricted my array to be array[row][n][3]

Post up a short example of your problem, that I can run, and let's see what the details are.

"cat" needs four bytes in C.

Hello adak and pytony! :)

thank you! I was able to solve that problem by getting a counter.

but now,i got another problem. I stored the result of comparing in adjacency list and now i want to group together rows that doesn't have a word in common.

Any ideas on how to do that? I tried if else but at the end random numbers popped out :(

Please help me. thank you very much :)

Build an index array of int's. After initializing the index[], 0-RowNum-1, Sort the index int's, according to how many matching words are on each line.

Although the data will not be moved, you can now print up the rows, ascending or descending, ranked by how many words matched in that row, using Rows[index].

Ever sorted by using an index[] array? It's a bit like magic when you first see it.

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.