Reading TEXT files
-
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
-
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
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