Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 22nd, 2005, 12:42 AM   #1
rsnegi
Newbie
 
Join Date: Jun 2005
Posts: 1
Rep Power: 0 rsnegi is on a distinguished road
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 ".."....
rsnegi is offline   Reply With Quote
Old Jun 22nd, 2005, 2:50 AM   #2
uman
Expert Programmer
 
Join Date: Dec 2004
Posts: 794
Rep Power: 5 uman is on a distinguished road
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.
uman is offline   Reply With Quote
Old Jun 22nd, 2005, 6:49 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 22nd, 2005, 7:08 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Jun 22nd, 2005, 7:39 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Jun 22nd, 2005, 1:27 PM   #6
Eggbert
Professional Programmer
 
Eggbert's Avatar
 
Join Date: Nov 2004
Posts: 250
Rep Power: 5 Eggbert is on a distinguished road
>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;
}
Eggbert is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 10:08 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC