Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. C / C++ / MFC
  3. Reading TEXT files

Reading TEXT files

Scheduled Pinned Locked Moved C / C++ / MFC
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Steve Lai
    wrote on last edited by
    #1

    Hi Everyone, I'm using Visual C++6 and have this problem and hope you can help me out! What I'm trying to do is to read data from a TEXT FILE and plug the values into a CString, which will be used as a SQL statement. For example, let's say I have the follwing data in a TEXT FILE: (the format used for the TEXT File is that the fist row will be the tablename, and then the following rows will be the field names and its attributes, and the field names and attributes will be separated by a comma) CustomerName CustID, INTEGER FirstName, STRING LastName, STRING etc... How can I read the values from the TEXT FILE one by one so at the end, I'll come up with a statement such as: SQL= "CREATE TABLE CustomerName(CustID INTEGER, FirstName STRING, LastName STRING, etc...)" I would like to put the reading of the values from the TEXT FILE into a loop so that no matter how many FIELDS the text files contain, the loop will be able to handle it and put all the values into a SQL statment. For example, if I have the following int the TEXT FILE: (3 FILEDS and 3 VALUES OF THE FIELD) CustomerName CustID, INTEGER FirstName, STRING LastName, STRING Then the following would occur: SQL="CREATE TABLE CustomerName(CustID INTEGER, FirstName STRING, LastName STRING)" BUT if I only have 1 FIELD AND VALUE: CustomerName CustID, INTEGER The reading of the TEXT FILE will be dynamic and the SQL would be: SQL="CREATE TABLE CustomerName(ID INTEGER)" Hope you can understand what I'm trying to do. I've tried to do it in a loop myself but I had some trouble, so If anyone has any solutions, PLMK! Thanks! Steve

    W 1 Reply Last reply
    0
    • S Steve Lai

      Hi Everyone, I'm using Visual C++6 and have this problem and hope you can help me out! What I'm trying to do is to read data from a TEXT FILE and plug the values into a CString, which will be used as a SQL statement. For example, let's say I have the follwing data in a TEXT FILE: (the format used for the TEXT File is that the fist row will be the tablename, and then the following rows will be the field names and its attributes, and the field names and attributes will be separated by a comma) CustomerName CustID, INTEGER FirstName, STRING LastName, STRING etc... How can I read the values from the TEXT FILE one by one so at the end, I'll come up with a statement such as: SQL= "CREATE TABLE CustomerName(CustID INTEGER, FirstName STRING, LastName STRING, etc...)" I would like to put the reading of the values from the TEXT FILE into a loop so that no matter how many FIELDS the text files contain, the loop will be able to handle it and put all the values into a SQL statment. For example, if I have the following int the TEXT FILE: (3 FILEDS and 3 VALUES OF THE FIELD) CustomerName CustID, INTEGER FirstName, STRING LastName, STRING Then the following would occur: SQL="CREATE TABLE CustomerName(CustID INTEGER, FirstName STRING, LastName STRING)" BUT if I only have 1 FIELD AND VALUE: CustomerName CustID, INTEGER The reading of the TEXT FILE will be dynamic and the SQL would be: SQL="CREATE TABLE CustomerName(ID INTEGER)" Hope you can understand what I'm trying to do. I've tried to do it in a loop myself but I had some trouble, so If anyone has any solutions, PLMK! Thanks! Steve

      W Offline
      W Offline
      wanderley
      wrote on last edited by
      #2

      Hi Steve, This is not the best code I've ever wrote, but I hope it can help you. :-) Assuming that the first line is always the table name, you could do something like this: --struct.txt CustomerName CustID, INTEGER FirstName, STRING --code CStdioFile f; CString sLine, sSql; int nLine = 0; sSql = "CREATE TABLE "; f.Open("struct.txt", CFile::modeRead); while (f.ReadString(sLine)) { if (sLine.GetLength()) { if (++nLine == 1) { sSql += sLine+'('; } else { sLine.Replace(",", ""); sSql += sLine+','; } } } sSql.SetAt(sSql.GetLength()-1, ')'); f.Close(); AfxMessageBox(sSql); Regards, Wanderley

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups