![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 1
Rep Power: 0
![]() |
how to read such type of files....
hi....anyone plese tell me how to read data from such kind of txt files..
"1",868000,3917200,"(868000.0,3917200.0)" "2",878000,3917200,"(878000.0,3917200.0)" "3",888000,3917200,"(888000.0,3917200.0)" "4",898000,3917200,"(898000.0,3917200.0)" "5",908000,3917200,"(908000.0,3917200.0)" "6",918000,3917200,"(918000.0,3917200.0)" "7",928000,3917200,"(928000.0,3917200.0)" "8",938000,3917200,"(938000.0,3917200.0)" "9",948000,3917200,"(948000.0,3917200.0)" "10",958000,3917200,"(958000.0,3917200.0)" "11",968000,3917200,"(968000.0,3917200.0)" "12",978000,3917200,"(978000.0,3917200.0)" "13",988000,3917200,"(988000.0,3917200.0)" "14",998000,3917200,"(998000.0,3917200.0)" "15",1008000,3917200,"(1008000.0,3917200.0)" "16",1018000,3917200,"(1018000.0,3917200.0)" "17",1028000,3917200,"(1028000.0,3917200.0)" "18",1038000,3917200,"(1038000.0,3917200.0)" "19",1048000,3917200,"(1048000.0,3917200.0)" i am required to read all elements provided within "..".... |
|
|
|
|
|
#2 |
|
Expert Programmer
Join Date: Dec 2004
Posts: 794
Rep Power: 5
![]() |
Is this a homework assignment? We don't do those here.
Read up on I/O in C++. Streams are covered in section 20 of Bjarne Stroustrup's The C++ Programming Language, if you have that. If not, use Google for now, and buy it as soon as you can. |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
One approach: read a line at a time, tokenize on commas, discard tokens which don't begin and end with a quotation mark.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
I know this is the C++ forum, but if it really doesn't matter in which language it is then I here's some Java code I had laying around:
import java.io.*;
import java.util.StringTokenizer;
class FillDatabase
{
private String line;
private BufferedReader reader;
private BufferedWriter writer;
public FillDatabase()
{
String phonenumber, name, address, zipcode, city;
int customerID = 1;
String newline;
try
{
reader = new BufferedReader(new FileReader("zeist.csv"));
writer = new BufferedWriter(new FileWriter("zeist.sql"));
while((line = reader.readLine()) != null)
{
StringTokenizer st = new StringTokenizer(line, ",");
phonenumber = st.nextToken();
name = st.nextToken();
address = st.nextToken();
zipcode = st.nextToken();
city = st.nextToken();
newline = "INSERT INTO customers(cus_id, cus_name, cus_address, cus_zipcode, cus_city)"
+ " VALUES ("
+ customerID++ + ", " + name + ", " + address + ", " + zipcode + ", " + city
+ ");";
writer.write(newline);
writer.newLine();
writer.flush();
}
reader.close();
writer.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
public static void main(String args[])
{
FillDatabase fillDB = new FillDatabase();
}
}Edit to follow DaWei's comments.
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Bear in mind that if it's supposed to be a .csv file, the numbers in parentheses should be treated as one token, as they're quoted. You really need to provide yourself a firm definition of precisely what is supposed to be what before you can design a solution. (Always define a solution before implementing it in code.)
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Professional Programmer
Join Date: Nov 2004
Posts: 250
Rep Power: 5
![]() |
>anyone plese tell me how to read data from such kind of txt files..
With a CSV parsing library. Comma separated values files are surprisingly difficult to parse manually if you follow the full CSV specification. A naive approach would ignore some of the details and still work for many cases, but break for anything unexpected or relying on certain CSV details such as the double double quote for embedded double quotes: #include <algorithm>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
vector<string> csv_parse ( const string& s );
int main()
{
string line ( "\"1\",868000,3917200,\"(868000.0,3917200.0)\"" );
cout<<"Original line: "<< line <<'\n';
cout<<"CSV separated line:\n";
vector<string> v = csv_parse ( line );
copy ( v.begin(), v.end(), ostream_iterator<string> ( cout, "\n" ) );
}
vector<string> csv_parse ( const string& s )
{
string::const_iterator first = s.begin();
string::const_iterator last = s.end();
vector<string> v;
string field;
while ( first != last ) {
switch ( *first ) {
case ',':
v.push_back ( field );
field.clear();
break;
case '"':
while ( ++first != last && *first != '"' )
field.push_back ( *first );
break;
default:
field.push_back ( *first );
break;
}
++first;
}
if ( field != "" )
v.push_back ( field );
return v;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|