bloggerads

2013年1月3日 星期四

C# : Call a exe file without a command prompt blinking

using System.Diagnostics;


private static int RunProcess(string processName, string arguments)
{
    Process process = new Process();
    process.StartInfo.FileName = processName;
    process.StartInfo.Arguments = arguments;
 //* 
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.CreateNoWindow = true;  //Without a command prompt blinking
 //&
    process.Start();
    process.WaitForExit();
    return process.ExitCode;
}

C# : Basic knowledge notes

// int to string

int iValue = 4;
string sResult = iValue.ToString();
string sResult = iValue.ToString("X16");  // int to Hex string

// string to int

string num = "-105";
int numVal = Int32.Parse(num);

// string array

string[] lines= new string[] { "Param1", "Param2" };

Array.Clear(lines, 0, lines.Length); // Clear all (Won't resize)

2013年1月2日 星期三

C# : Write and Read Binary file

using System.IO;
byte[] content = new byte[] {1, 2};
File.WriteAllBytes(@"E:\12.bin", content);
byte[] bytes = File.ReadAllBytes(@"E:\12.bin");
Console.WriteLine("{0:x3}", bytes[0]); // Hex format and 3 digits
Console.WriteLine("{0:D2}", bytes[1]); // Dec format and 2 digits

2013年1月1日 星期二

C# : Write TXT file

Write a struct to a TXT file

struct test{
    public char a;
    public int x;
    public int y;
    public int z;
}

static void Main(string[] args)
{
    test[] aa = new test[] {
        new test { a = 'a', x = 1, y = 2, z = 3 },
        new test { a = 'b', x = 2, y = 3, z = 4 }
    };
    string path = System.IO.Directory.GetCurrentDirectory() + "\\go.txt";
    
    using (System.IO.StreamWriter file = new System.IO.StreamWriter(path))
    {
        foreach (test x in aa)
        {
            file.WriteLine("{0} {1} {1} {3}", x.a.ToString(), x.x, x.y, x.z);
        }
    }

}

C# : Write / Read CSV file sample code

Write an array to test.csv

static void Main(string[] args)
{
    string filePath = System.IO.Directory.GetCurrentDirectory() + @"\test.csv";

    string[] myDataArray = new string[] { "123", "abc", "ddd"};

    File.WriteAllText(filePath, string.Join(",", myDataArray));   
}


Read  test.csv

static void Main(string[] args)
{
    string filePath = System.IO.Directory.GetCurrentDirectory() + @"\test.csv";

    var item = new List<string>();

    foreach (var line in File.ReadLines(filePath))
    {
        var array = line.Split(',');
        foreach (string idx in array)
            item.Add(idx);
    }

    string[] strArray = item.ToArray();

    foreach (string ss in strArray)
    {
        Console.WriteLine("{0}", ss);
    }
    Console.ReadLine();
}




2012年10月21日 星期日

UBUNTU 12.04 LTS @ ASPIRE D270 重灌心得

目標: 用Ubuntu寫C++,並編譯


趁著周末,破釜沉舟的把小筆電Windows 7 stater給刪了,灌了Ubuntu來使用,沒想到最新版12.10竟然無法灌成功,開機都時後都會hang在進入Shell前。(無法進入式窗)

後來google一下發現大家都使用12.04 LTS版。網路上說也有人灌成功,我就死馬當活馬醫馬上灌灌看。

我的小筆電是 ASPIRE ONE D270
<灌系統>
我的步驟是:
1. 先下載ubuntu-12.04.1-desktop-i386.iso  (Ubuntu的映像檔)
2. 再下載Universal-USB-Installer-1.9.1.3.exe (將映像檔做成USB開機灌)

基本上就是一直下一步,就會灌成功了。但卻無法調整螢幕亮度。

<調整螢幕亮度>
首先開啟X window(Ubuntu的Command視窗,hotkey是ctrl+alt+t),鍵入
sudo  setpci  -s  "00:02.0"  F4.B=35
上面的35是螢幕亮度,可以自己調整,但下次開機又會變回來,因此我們必須在開機跑流程時加入這行指令。鍵入
sudo  gedit  /etc/init.d/rc.local
以文字編輯檔開啟開機的流程檔並將sudo  setpci  -s  "00:02.0"  F4.B=35貼到最後,下次開機就會保持這個亮度值了

<更改輸入法為中文>
再來就是中文輸入,請在system settings找 language support並在install/remove languages選中文。這樣還不行,我之後還在ubuntu software center 搜尋ibus下載input method(im-config)才可以成功輸入中文。

<寫個C++程式並編譯他>
1.編譯器請在Ubuntu software center搜尋G++,找到GNU C++ compiler並下載,完成後重開
2.編輯器我用的是Code::Blocks IDE,就是在這個介面下寫程式

<PS>
在Ubuntu下,路徑是有分大小寫的哦

2011年8月20日 星期六

C/C++ 的優先序


雖然到處都用括號括起來順序就一定不會錯,但是這樣寫code實在不方便閱讀
把優先序放在這邊方便參考

http://www.csee.umbc.edu/courses/104/fall06/burt/precedenceTable.html


LevelSymbolDescriptionAssociativity
1++
--
( )
[ ]
->
.
Prefix increment
Prefix decrement
Function call and subexpression
Array subscript
Structure pointer
Structure member
left to right
2!
~
-
+
(type)
*
&
sizeof
Logical negation
1's complement
Unary negation
Unary plus
Type cast
Pointer dereference
Address of
Size of
right to left
3*
/
%
Multiplication
Division
Modulus (integer remainder)
left to right
4+
-
Addition
Subtraction
left to right
5<<
>>
Bitwise left shift
Bitwise right shift
left to right
6<
<=
>
>=
Less than
Less than or equal to
Greater than
Greater than or equal to
left to right
7==
!=
Equal test
Not equal test
left to right
8&Bitwise ANDleft to right
9^Bitwise exclusive OR (XOR)left to right
10|Bitwise inclusive ORleft to right
11&&Logical ANDleft to right
12||Logical inclusive ORleft to right
13?:Conditional testright to left
14=
+=
-=
*=
/=
%=
<<=
>>=
&=
^=
|
Assignment
Compound add
Compound subtract
Compound multiply
Compound divide
Compound modulus
Compound bitwise left shift
Compound bitwise right shift
Compound bise AND
Compound bitwise exclusive OR
Compound bitwise inclusive OR
right to left
15,
++
--
Sequence point (list separator)
Postfix increment
Postfix decrement
left to right