14 Replies - 6020 Views - Last Post: 28 December 2010 - 11:42 PM Rate Topic: -----

#1 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

[C] Reading multiple lines from text file

Posted 26 December 2010 - 04:23 PM

I have a wordlist with one word on each line and am trying to work out how to read it.

What would be the best way to do this? If the wordlist is quite big how would I solve this?

My attempt so far:

/*
Name: read.c
*/
#include <stdio.h>
#include <stdlib.h>
#define HUGESIZE 5000000

int main(void)
{
	char words[HUGESIZE];
	FILE *fileptr;
	fileptr = fopen("C:/test.txt", "r");
	
	if(fileptr != NULL)
	{
		//Read file line by line somehow.
	}
	else
	{
		printf("Failed to read file.\n");
		exit(EXIT_FAILURE);
	}

	return 0;
}





Is This A Good Question/Topic? 0
  • +

Replies To: [C] Reading multiple lines from text file

#2 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: [C] Reading multiple lines from text file

Posted 26 December 2010 - 04:34 PM

Read Me
Was This Post Helpful? 0
  • +
  • -

#3 Slumdog  Icon User is offline

  • D.I.C Head

Reputation: 34
  • View blog
  • Posts: 116
  • Joined: 26-November 10

Re: [C] Reading multiple lines from text file

Posted 26 December 2010 - 06:18 PM

1. fopen the file.
2. while(fgets(buffer, size, file) != NULL)
3. printf("%s", buffer);
4. ???
5. Profit
Was This Post Helpful? 2
  • +
  • -

#4 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C] Reading multiple lines from text file

Posted 26 December 2010 - 07:01 PM

View PostJackOfAllTrades, on 26 December 2010 - 03:34 PM, said:



I got a bunch of errors when trying to compile that?

Using Microsft Visual C++ 2010 Express.
Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: [C] Reading multiple lines from text file

Posted 26 December 2010 - 10:43 PM

> I got a bunch of errors when trying to compile that?
Well I'm guessing you named your source files prog.cpp, because that is the VStudio default.

But this means you're compiling C++ (but learning C).
Whilst they have a common root, both languages are not easily cross-compiled without a lot of problems.

If you make sure your source files are named prog.c, then you will be compiling C.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is online

  • Saucy!
  • member icon

Reputation: 5951
  • View blog
  • Posts: 23,214
  • Joined: 23-August 08

Re: [C] Reading multiple lines from text file

Posted 27 December 2010 - 05:17 AM

View PostMatty919, on 26 December 2010 - 08:01 PM, said:

View PostJackOfAllTrades, on 26 December 2010 - 03:34 PM, said:



I got a bunch of errors when trying to compile that?

Using Microsft Visual C++ 2010 Express.


What Salem_c said, but also, I didn't mean copy and paste the code. I meant READ the code. Learn how it works. It demonstrates everything you need to do.
Was This Post Helpful? 1
  • +
  • -

#7 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 04:55 AM

This works:

void readfromfile(FILE *fileptr, char *word[], int *length2)
{
	char buffer[BUFSIZ];
	int length, x = 0, y;

	if(fileptr != NULL)
	{
		while(fgets(buffer, BUFSIZ, fileptr) != NULL)
		{
			length = strlen(buffer);
			//words[x] = malloc(length*sizeof(char));
			word[x] = malloc(length);
			for(y=0; y < (length - 1); y++)
			{
				word[x][y] = buffer[y];
			}
			x++;
		}
	}
	else
	{
		printf("Failed to read file.\n");
		exit(EXIT_FAILURE);
	}
}


But this doesn't, it crashes on runtime:

void read(FILE *fileptr, char *words[])
{
	char buffer[BUFSIZ];
	int i = 0;
	
	if(fileptr != NULL)
	{
		while(fgets(buffer, BUFSIZ, fileptr) != NULL)
		{
			strncat(words[i], buffer, strlen(buffer));
			i++;
		}
	}
	else
	{
		printf("Failed to read file.\n");
		exit(EXIT_FAILURE);
	}
}


Why doesn't the second version work?
Was This Post Helpful? 0
  • +
  • -

#8 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 05:24 AM

Because strncat cannot append a \0 given your usage - namely strlen() as the 3rd argument. Also, strncat() first tries to find a \0 in the current string so it can append to it.

strlen(x)+1 would mean you copied the \0.

Also, you didn't show how you declared your words array. Given your first example, it suggest that each word needs to be allocated to begin with.
Was This Post Helpful? 1
  • +
  • -

#9 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 05:26 AM

Did you allocate any memory for words[]?

It is an array of pointers that you are trying to store character strings in.

Have you set aside any memory for the strings?
Was This Post Helpful? 1
  • +
  • -

#10 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 05:32 AM

int main(void)
{
	char *wordlist[MAXWORDS];
	char *searchwords[NOOFSW];
	FILE *fileptr;
	fileptr = fopen("C:/wordlist.txt", "r");
	read(fileptr, &wordlist[0]);
}

void read(FILE *fileptr, char *words[])
{
	char buffer[BUFSIZ] = {'\0'};
	int i = 0;
	
	if(fileptr != NULL)
	{
		while(fgets(buffer, BUFSIZ, fileptr) != NULL)
		{
			words[i] = malloc(strlen(buffer));
			strncat(words[i], buffer, (strlen(buffer) + 1));
			i++;
		}
	}
	else
	{
		printf("Failed to read file.\n");
		exit(EXIT_FAILURE);
	}
}


Still crashes :crazy:

This post has been edited by Matty919: 28 December 2010 - 05:34 AM

Was This Post Helpful? 0
  • +
  • -

#11 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 05:46 AM

1. You didn't malloc enough memory, you need strlen() + 1
2. ALL str..cat functions begin by looking for the \0, except malloc returns memory filled with garbage.

Since you've already measured the amount of memory needed quite carefully, you may as well replace the strncat with a simple strcpy()
Was This Post Helpful? 2
  • +
  • -

#12 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 11:40 AM

View PostSalem_c, on 28 December 2010 - 04:46 AM, said:

1. You didn't malloc enough memory, you need strlen() + 1
2. ALL str..cat functions begin by looking for the \0, except malloc returns memory filled with garbage.

Since you've already measured the amount of memory needed quite carefully, you may as well replace the strncat with a simple strcpy()


Thanks for the help, using strcpy works now but I don't understand why strcat doesn't? Also I thought strcpy overwrite was was originaly in the destination but it doesn't so whats the difference between it and strcat?
Was This Post Helpful? 0
  • +
  • -

#13 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 02:14 PM

strcat doesn't work in this instance because the memory returned by malloc is uninitialised.

Consider str = "word";
p = malloc(strlen(str)+1);
strcat(p, str);

Now if p points to "random_junk\0", then the strcat is going to result in "random_junkword\0". The first obvious mistake is that this overflows your buffer. The second point is that the \0 strcat finds need not even be within bounds of the memory you allocated.

Now with strcpy instead, "random_junk\0" gets overwritten with "word\0m_junk" (the first 5 bytes only), which is what you want AND within the bounds of the memory you allocated.
Was This Post Helpful? 1
  • +
  • -

#14 Matty919  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 83
  • Joined: 10-May 09

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 11:04 PM

Another question:

I'm trying to find the number of elements of:

char *wordlist[MAXWORDS]

which actually have words stored in them.

How would I do this?

I tried:

int findlength(char *words[])
{
	return sizeof(words)/sizeof(char *);
}



But this function keeps returning 1...
Was This Post Helpful? 0
  • +
  • -

#15 bnc  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 108
  • Joined: 18-March 10

Re: [C] Reading multiple lines from text file

Posted 28 December 2010 - 11:42 PM

View PostMatty919, on 28 December 2010 - 10:04 PM, said:

Another question:

I'm trying to find the number of elements of:

char *wordlist[MAXWORDS]

which actually have words stored in them.

How would I do this?

I tried:

int findlength(char *words[])
{
	return sizeof(words)/sizeof(char *);
}



But this function keeps returning 1...



why can't you do like this?

        int no_of_words;
        read(fileptr, wordlist,no_of_words);
        printf("words are \n");
        for(int i = 0; i < no_of_words; ++i)
        {
                printf("*%s*\n",wordlist[i]);
        }

void read(FILE *fileptr, char *words[],int& n)
{
        char buffer[BUFSIZ] = {'\0'};
        int i = 0;
        if(fileptr != NULL)
        {
                while(fgets(buffer, BUFSIZ, fileptr) != NULL)
                {
                        words[i] = (char*) malloc(strlen(buffer + 1));
                        strncat(words[i], buffer, (strlen(buffer) + 1));
                        i++;
                }
                n = i;
        }
        else
        {
                printf("Failed to read file.\n");
                exit(EXIT_FAILURE);
        }
}




Was This Post Helpful? 0
  • +
  • -

Page 1 of 1