Click here to Skip to main content
15,888,287 members
Home / Discussions / C#
   

C#

 
QuestionODBC Problem Pin
telconstar996-Nov-05 10:34
telconstar996-Nov-05 10:34 
AnswerRe: ODBC Problem Pin
Corinna John6-Nov-05 20:34
Corinna John6-Nov-05 20:34 
GeneralRe: ODBC Problem Pin
telconstar997-Nov-05 5:30
telconstar997-Nov-05 5:30 
GeneralRe: ODBC Problem Pin
Corinna John7-Nov-05 5:47
Corinna John7-Nov-05 5:47 
Questionhow to create iso image in c#? Pin
hdv2126-Nov-05 10:34
hdv2126-Nov-05 10:34 
AnswerRe: how to create iso image in c#? Pin
g00fyman6-Nov-05 13:49
g00fyman6-Nov-05 13:49 
GeneralRe: how to create iso image in c#? Pin
lmoelleb6-Nov-05 21:14
lmoelleb6-Nov-05 21:14 
QuestionHelp on file processing Pin
Tugbay Sahin6-Nov-05 9:55
Tugbay Sahin6-Nov-05 9:55 
Hello..

I simply want to develop a stock program but somewhere I make smth. wrong and I cannot say where.. My program seems write into file in binary mode but connot recover the data correct.. The problem is , I enter 3 samples fırst.. then I want to edıt 3.rd one , when ın debug mode , I can get first 2 item's data but when 3rd , It throws an exception "Unable to read beyond the end of the stream." Can anyone help me what is wrong with my code? Thanks..



using System;
using System.Data;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace fileodevi
{

public struct type//yenı urun ıcın kullanılacak struct type
{
public string name;
public double price;
public int stok;
public const int SIZE = 36;
}


class Class1
{
private const string fileName = "file1.dat";
private BinaryWriter writer = null;
private BinaryReader reader = null;
public FileStream file = null;//global deklerasyon , daha sonra her yerde kullanalım dıye..
public Class1()
{
do
{
try
{
file = new FileStream(fileName,FileMode.OpenOrCreate);
Console.WriteLine("File created(if NOT exists / Opened(if exists)");
break;
}
catch(Exception exp)
{
Console.WriteLine(exp);
file.Close();
}
}while(true);
file.Close();
}

public void enterNewProduct()
{
type newProduct = new type();
do
{
try
{
file = new FileStream(fileName , FileMode.Append , FileAccess.Write);
Console.WriteLine("File opened for writing new entry");
break;
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}while(true);
//urun adı
Console.WriteLine("Enter name of the new product :");
Console.WriteLine("WARNING name MUST be at most 20 characters!!\n");
do
{
newProduct.name = Console.ReadLine();
if(newProduct.name.Length < 20)
{
newProduct.name += new string(' ',20-newProduct.name.Length);
}
}while(newProduct.name.Length > 20);
//urun fıyatı
Console.WriteLine("Enter Price of the new product :");
do
{
try
{
newProduct.price = double.Parse(Console.ReadLine());
break;
}
catch(FormatException e)
{
Console.WriteLine(e);
}
}while(true);
//urun adedı
Console.WriteLine("Enter amount of the product :");
do
{
try
{
newProduct.stok = Int32.Parse(Console.ReadLine());
break;
}
catch(FormatException ex)
{
Console.WriteLine(ex);
}
}while(true);

//urunu fıle a kaydetme
do
{
try
{


writer = new BinaryWriter(file);
writer.Write(newProduct.name);
writer.Write(newProduct.price);
writer.Write(newProduct.stok);

Console.WriteLine("Product successfully entered to file");
writer.Close();
file.Close();
Console.WriteLine("File closed");
break;
}
catch(Exception b)
{
Console.WriteLine(b);
}
}while(true);


}

public void editProduct(string name)
{
type pro = new type();
if(name.Length < 20)
{
name += new string(' ' ,(20-name.Length));
}
do
{
try
{
file = new FileStream(fileName,FileMode.Open,FileAccess.ReadWrite);
Console.WriteLine("File opened for Editing");

break;
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}while(true);
reader = new BinaryReader(file);
for(int b = 0;;b++)
{

pro.name = reader.ReadString();
pro.price = reader.ReadDouble();
pro.stok = reader.ReadInt32();
int sel = 0;
try
{
if(string.Equals(name , pro.name))
{
reader.Close();
Console.WriteLine("What attirbute dou you wish to change ? :");
Console.WriteLine(" 1) Name\n 2) Price\n 3)Amaount in stock");
sel = Int32.Parse(Console.ReadLine());
switch(sel)
{
case 1:
Console.WriteLine("Enter new Name of the product :");
Console.WriteLine("The name must be at most 20 characters\n");
do
{
pro.name = Console.ReadLine();
if(pro.name.Length < 20)
{
pro.name += new string(' ',(20-pro.name.Length));
Console.WriteLine(pro.name.Length.ToString());
}
}while(pro.name.Length > 20);
break;
case 2:
Console.WriteLine("Enter new Price of the product :");
do
{
try
{
pro.price = double.Parse(Console.ReadLine());
break;
}
catch(Exception ex)
{
Console.WriteLine("Invalid amount try again..");
}
}while(true);
break;
case 3:
Console.WriteLine("Enter new stock number :");
do
{
try
{
pro.stok = Int32.Parse(Console.ReadLine());
break;
}
catch(Exception ex)
{
Console.WriteLine("Invalid amount try again..");
}
}while(true);
break;
}
writer = new BinaryWriter(file);
writer.Seek(b*(type.SIZE),SeekOrigin.Current);
try
{
writer.Write(pro.name);
writer.Write(pro.price);
writer.Write(pro.stok);
writer.Close();
file.Close();
Console.WriteLine("File Closed , writer closed");

}
catch(Exception ex)
{
Console.WriteLine("Error writting");
}
break;

}
else
{
file.Seek(b*(type.SIZE),SeekOrigin.Current);
}
}
catch(Exception ex)//File ıcınde obje bulamadıgı zaman veya okurken bı hata ıle karsılasırsa...
{
Console.WriteLine("No such Item Found...");
file.Close();
break;
}
}
}

[STAThread]
static void Main(string[] args)
{
char sel ;
Class1 c = new Class1();
while(true)
{
Console.WriteLine("A) To enter a new product");
Console.WriteLine("B) Edit a product");
Console.WriteLine("C) Order a product");
Console.WriteLine("D) Exit program");
do
{
try
{
sel = char.Parse(Console.ReadLine().ToLower());
break;

}
catch(Exception)
{
Console.WriteLine("The Input command is invalid.Try Again..");
}
}while(true);
if( sel == 'a')
{
c.enterNewProduct();
}
if(sel == 'd')
{
break;
}
if(sel == 'b')
{
Console.WriteLine("Enter the name of the product you wish yo edit :");
c.editProduct(Console.ReadLine());

}
}
}
}
}


Revelation 22:13

-- modified at 15:58 Sunday 6th November, 2005
GeneralRe: Help on file processing Pin
Guffa6-Nov-05 12:34
Guffa6-Nov-05 12:34 
QuestionComboBox - set value Pin
Jens19726-Nov-05 9:31
Jens19726-Nov-05 9:31 
Questionhow to get serial number of cd in c# ? Pin
hdv2126-Nov-05 8:53
hdv2126-Nov-05 8:53 
QuestionProblem in recalling value with array Pin
PearHK6-Nov-05 8:52
PearHK6-Nov-05 8:52 
AnswerRe: Problem in recalling value with array Pin
Christian Graus6-Nov-05 11:58
protectorChristian Graus6-Nov-05 11:58 
QuestionLabel doesn&#180;t show up on form Pin
naglbitur6-Nov-05 8:35
naglbitur6-Nov-05 8:35 
AnswerRe: Label doesn&amp;#180;t show up on form Pin
Christian Graus6-Nov-05 11:59
protectorChristian Graus6-Nov-05 11:59 
QuestionSpeed up? Pin
Lju26-Nov-05 8:05
Lju26-Nov-05 8:05 
QuestionCom+ Problem Pin
webhay6-Nov-05 4:15
webhay6-Nov-05 4:15 
AnswerRe: Com+ Problem Pin
Heath Stewart6-Nov-05 6:21
protectorHeath Stewart6-Nov-05 6:21 
QuestionCom+ Problem Pin
webhay6-Nov-05 4:12
webhay6-Nov-05 4:12 
QuestionCom+ Problem Pin
webhay6-Nov-05 4:12
webhay6-Nov-05 4:12 
QuestionC# TreeView and Outlook Folders Pin
_Goose6-Nov-05 3:57
_Goose6-Nov-05 3:57 
AnswerRe: C# TreeView and Outlook Folders Pin
_Goose6-Nov-05 4:19
_Goose6-Nov-05 4:19 
QuestionTransparent Custom Controls Pin
Johnny Hooyberghs6-Nov-05 2:30
Johnny Hooyberghs6-Nov-05 2:30 
AnswerRe: Transparent Custom Controls Pin
IdUnknown6-Nov-05 3:54
IdUnknown6-Nov-05 3:54 
GeneralRe: Transparent Custom Controls Pin
Johnny Hooyberghs6-Nov-05 4:33
Johnny Hooyberghs6-Nov-05 4:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.