Read Text File In C#

  1. Read Data From Text File In C#
  2. Read Text File In C#
  3. Cached
  4. C# Read In Text File To String

Apr 02, 2019 Call open method to open a file “tpoint.txt” to perform read operation using object newfile. If file is open then Declare a string “tp”. Read all data of file object newfile using getline method and put it into the string tp. Print the data of string tp. Close the file object newfile using close method. The putc function is used to write a character to a file whereas the fputs function is used to write a line of text into a file. The syntax for putc is: int putc (char c,FILE. fp); where c.

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:

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!

Editedby frankchester because:n/a
  • 3 Contributors
  • forum9 Replies
  • 28,527 Views
  • 16 Hours Discussion Span
  • commentLatest PostLatest Postby frankchester

Recommended Answers

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(...).

Read Data From Text File In C#

scanf() - reads data from keyboard.

Jump to Post

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);

TextC# read in a text fileRead a text file in c#Read Text File In C#

// now do something with text[][]

Jump to Post

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

Jump to Post

Read Text File In C#

All 9 Replies

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

Cached

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

C# Read In Text File To String

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