Hi, I'm a newbie to programming in general and I'm really struggling with working out how on earth to do this!

I am aware of how to open the stream and read the text file using fscanf, however I want to store the text in that file into a string and I'm not sure how to configure the fscanf to do this.

So far I have:

{

FILE *file_in;

char text;

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

fscanf(file_in, "...?"..... - this is the bit where I'm stuck for what to put?

fclose(file_in)

return 0;

}

I know this probably sounds really silly, but I can't quite figure out what the fscanf is actually doing, and how best to store the data.

Would it be best to store the data into an array? And how would I go about that?

This is part of a bigger program which I'm trying to write, which will then ask me to encrypt this text using a Caesar cipher. I'm thinking a string would be easier than an array?

Thanks!

Edited 6 Years Ago by frankchester: n/a

You need a clear understanding of C-stlye string. A string in C is represented as character array terminated by NULL '\0' character.

fscanf(...) statement reads first string from the file until a space or newline is encountered. It works similar to scanf(...).

scanf() - reads data from keyboard.
fscanf() - reads data from file stream.

Ah ok, so if the text file is more than one line, it will read the first, store it, move to the next and then erase the old one, won't it? So I will only get the last line stored in the string. I sort of understand it :P

Would it be better to put a loop in then? So that it will store the whole file until it reaches the EOF? I'm just not sure how to go about it :S

Dear friend, I think this may help you...

char text[100][50]; // assuming you have 100 words in the file each of max length 50
FILE *fp=fopen("filename.txt", "r");
for(int i=0; ferror(fp) == 0; i++)
fscanf(fp, "%s", text);

// now do something with text[][]

Dear friend, I think this may help you...

char text[100][50]; // assuming you have 100 words in the file each of max length 50
FILE *fp=fopen("filename.txt", "r");
for(int i=0; ferror(fp) == 0; i++)
fscanf(fp, "%s", text);

// now do something with text[][]

Hi, thats just it! Could you explain to me what the ferror bit means though? Its not something I've seen before.

If you want to store entire file in an string array, do this.

char text[1000];
FILE *fp=fopen("filename", "r");
int i=0;
while(feof(fp))
     text[i++] = fgetc(fp);
text[i]='\0';

ferror() - return non-zero value if an error is encountered (say EOF, DISK FULL etc).

Hi everyone, thanks for your replies so far.

I've managed to get a working piece of code using the examples given, however I've run into a problem. Here is the code:

/*Program to read in text file and store data in a string*/

#include<stdio.h>

int main()
{
	/*declare and initialise variable*/
	char message[100];
	int i=0;

	FILE *file_in;
	file_in=fopen("text.txt", "r");

	/*stores and prints the data from the string*/
	fscanf(file_in,"%s", message);
	printf("%s\n", message);

    return 0;


}

The problem is that fscanf doesn't like spaces, so it sees the sentence I have and only stores the first word. The program will store and print the first word fine.

I'm thinking I need to use something different fget or fread or something, but have no idea how to implement that into the program?

Thanks.

Hi everyone, thanks for your replies so far.

I've managed to get a working piece of code using the examples given, however I've run into a problem. Here is the code:

(...)

The problem is that fscanf doesn't like spaces, so it sees the sentence I have and only stores the first word. The program will store and print the first word fine.

I'm thinking I need to use something different fget or fread or something, but have no idea how to implement that into the program?

Thanks.

You should use:

fgets(char*,int,FILE*);

The first argument is the variable that will hold the resulting string, the 2nd is the maximum number of chars, and the 3rd is the pointer to the file.
fgets read until it finds a newline. It returns 0 when it reachs the end of file.

Example:

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

int main()
{
    /*declare and initialise variable*/
    char message[10][150],buffer[150];
    int i=0;

    FILE *file_in;
    file_in=fopen("text.txt", "r");

    /*stores and prints the data from the string*/
    while(fgets(buffer,150,file_in)){
        strcpy(message[i],buffer);
        printf("Line %d: %s",i,message[i]);
        i++;
    }
    getchar();
    return 0;
}

Edited 6 Years Ago by darkbreaker: n/a

Thanks, that worked! I've got it working now :)

I have so many more things I need to work out though, next I have to work out how to get the user to choose the filename. I've scanned in the filename from the keyboard but I don't know how to get it into the fopen bit :(

This question has already been answered. Start a new discussion instead.