Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read character in a file 1 by 1 c#

Tags:

c#

filestream

I want my program to read a text file all characters 1 by 1 and whereever it finds a double-quote ("), it adds a semicolon before that inverted comma. For eg we have a paragraph in a text file as follow:

This is a paragraph which conains lots and lots of characters and some names and dates. My name "Sam" i was born at "12:00" "noon". I live in "anyplace" .

Now I want the output to be as follows:

This is a paragraph which conains lots and lots of characters and some names and dates. My name ;"Sam;" i was born at ;"12:00;" ;"noon;". I live in ;"anyplace;" .

It should open the file using file stream then reads character and then adds semicolon where it finds quotes. And the output should be equal to textbox1.Text.

This is my code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            
            char ch;
            int Tchar = 0;
            StreamReader reader;
            reader = new StreamReader(@"C:\Users\user1\Documents\data.txt");
            do
            {
                ch = (char)reader.Read();
                Console.Write(ch);
                if (Convert.ToInt32(ch) == 34)
                {
                    Console.Write(@";");
                }
                Tchar++;
            } while (!reader.EndOfStream);
            reader.Close();
            reader.Dispose();
            Console.WriteLine(" ");
            Console.WriteLine(Tchar.ToString() + " characters");
            Console.ReadLine();
        }
    }
}

This is the output:

This is a paragraph which conains lots and lots of characters and some names and dates. My name ";Sam"; i was born at ";12:00"; ";noon";. I live in ";anyplace"; . 154 characters

I want that semicolon before the quotes. Any help would be appreciated. Thanks!

like image 465
Sam Avatar asked May 17 '13 21:05

Sam


People also ask

How do you read a character from file in C?

fgetc()– This function is used to read a single character from the file. fgets()– This function is used to read strings from files. fscanf()– This function is used to read formatted input from a file.

How do I read a single character from a file?

If you prefer to read one character at a time (including whitespace characters), you can use the get operation: char ch; while (inFile. get(c)) { ... } In this example, each time the while loop condition is evaluated, the next character in the input file is read into variable ch.

How can we read and write a single char for a file in C?

We use the getc() and putc() I/O functions to read a character from a file and write a character to a file respectively. Syntax of getc: char ch = getc(fptr);


2 Answers

Swap the order of the operations:

    if (Convert.ToInt32(ch) == 34)
    {
        Console.Write(@";");
    }
    Console.Write(ch);

e.g. don't write the original character until AFTER you've decided to output a semicolon or not.

like image 113
Marc B Avatar answered Sep 21 '22 20:09

Marc B


Try ch = (char)reader.Peek();

This will read tell you the next character without reading it. You can then use this to check if it is a " or not an insert : accordingly

if (Convert.ToInt32((char)read.Peek()) == 34) Console.Write(@";")
like image 33
SkeetJon Avatar answered Sep 24 '22 20:09

SkeetJon