Results 1 to 2 of 2

Thread: ต้องการเขียนภาษาC เพื่ออ่านfile แล้วนับบรรทัด

  1. #1
    Junior Member
    Join Date
    Dec 2006
    Posts
    0


    ต้องการเขียนภาษาC เพื่ออ่านfile แล้วนับบรรทัด

    รายละเอียดนะครับ จะเป็นว่ามีอยู่ 1 file ที่เขียนจากภาษา
    C เลยแล้ว save ไว้ ผมต้องการเขียนโปรแกรมจาก c หรือpascal ก็ได้เพื่ออ่านไฟล์นั้นแล้วบอกถึงจำนวน บรรทัดนะครับ ไม่ทราบว่าจะต้องมี function อะไรบ้างครับ
    ขอความกรุณาด้วยนะครับ ขอบคุณครับ

  2. #2
    Administrator asylu3's Avatar
    Join Date
    Jun 2000
    Location
    Thailand
    Posts
    3,557


    Re: ต้องการเขียนภาษาC เพื่ออ่านfile แล้วนับบรรทัด

    เขียนโดย VC++ นะครับ ถ้าต้องการอ่านไฟล์ก็แก้ตรง #define เองนะครับว่าไฟล์ของคุณชื่ออะไร
    [src]
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <conio.h>

    using namespace std;
    #define openfile "test.txt"

    int main(){

    char buffer[255];
    int counter=0;
    ofstream outfile(openfile,ios:ut);

    //ทำการเขียนข้อความ "test" ลงใน file
    for (int i=0;i<20;++i)
    {
    outfile<<" "<<endl;
    outfile<<"test"<<i<<endl;

    }
    outfile.close();

    //เริ่มทำการเปิดไฟล์และนับบรรทัด
    ifstream infile(openfile,ios::in);
    while(!infile.eof())
    {
    infile.getline(buffer,100);
    counter++;
    }

    cout<<"Number of line is "<<counter<<endl;
    getch();
    }
    [/src]

Members who have read this thread : 0

Actions : (View-Readers)

There are no names to display.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •