while(getline())循环从不读取文本文件时执行?

时间:2019-02-10 00:31:47

标签: c++ while-loop fstream ifstream

我正在处理一个更大的项目,但是无法遍历文本文件中每一行文本的简单循环:

import pandas as pd
import pyodbc
from sqlalchemy import __version__ as sa_version, create_engine, text
import sys
import urllib

print(sys.version)
# 3.7.2 (tags/v3.7.2:9a3ffc0492, Dec 23 2018, 23:09:28) [MSC v.1916 64 bit (AMD64)]
print(f'SQLAlchemy {sa_version}, pandas {pd.__version__}, pyodbc {pyodbc.version}')
# SQLAlchemy 1.3.0b2, pandas 0.24.1, pyodbc 4.0.25

connection_string = (
    r'DRIVER=ODBC Driver 17 for SQL Server;'
    r'SERVER=(local)\SQLEXPRESS;'
    r'DATABASE=myDb;'
    r'Trusted_Connection=Yes;'
    r'UseFMTONLY=Yes;'
)
sqlalchemy_url = (
    'mssql+pyodbc:///?odbc_connect=' \
    + urllib.parse.quote_plus(connection_string)
)
engine = create_engine(sqlalchemy_url, fast_executemany=True)

# set up test environment
if 0 == engine.execute("SELECT COUNT(*) FROM sys.tables WHERE name='actual_table';").fetchone()[0] :
    engine.execute("""\
    CREATE TABLE actual_table (
        institution_no VARCHAR(3), 
        transit_no VARCHAR(5), 
        branch_name VARCHAR(50),
        CONSTRAINT PK_actual_table PRIMARY KEY CLUSTERED 
            (institution_no, transit_no));
        """)
else:
    # clear out previous test data
    engine.execute(text("TRUNCATE TABLE actual_table;").execution_options(autocommit=True))
# actual_table initial state
engine.execute("""\
INSERT INTO actual_table (institution_no, transit_no, branch_name) VALUES 
    ('002', '45678', 'Scotiabank branch #45678 - *** UPDATE NEEDED ***'),
    ('003', '67890', 'RBC branch #67890 - Sudbury, ON');
""")

# test data to be updated or inserted
update_columns = ['institution_no', 'transit_no', 'branch_name']
update_data = [
    ['004', '12345', 'TD branch #12345 - London, ON'],
    ['002', '45678', 'Scotiabank branch #45678 - Timmins, ON'],
    ['004', '34567', 'TD branch #34567 - Toronto, ON'],
]
df_update = pd.DataFrame(update_data, columns=update_columns)

# Here's where the real work begins ...
#
# Step 1: upload update data
df_update.to_sql('#update_table', engine, index=None)
#
# Step 2: perform the "upsert"
sql = """\
SET NOCOUNT ON;
DECLARE @rows_updated INT = 0;
DECLARE @rows_inserted INT = 0;

UPDATE a SET a.branch_name = u.branch_name
    FROM actual_table a INNER JOIN #update_table u
        ON a.institution_no = u.institution_no 
            AND a.transit_no = u.transit_no;
SELECT @rows_updated = @@ROWCOUNT;

INSERT INTO actual_table (institution_no, transit_no, branch_name)
    SELECT institution_no, transit_no, branch_name
    FROM #update_table u
    WHERE NOT EXISTS (
        SELECT * FROM actual_table
        WHERE institution_no = u.institution_no
            AND transit_no = u.transit_no
    );
SELECT @rows_inserted = @@ROWCOUNT;

SELECT @rows_updated AS rows_updated, @rows_inserted AS rows_inserted;
"""
cnxn = engine.raw_connection()
result = cnxn.execute(sql).fetchone()
cnxn.commit()
print(f'{result[0]} row(s) updated, {result[1]} row(s) inserted')
# 1 row(s) updated, 2 row(s) inserted

# verify results
print(cnxn.execute("SELECT * FROM actual_table").fetchall())
# [('002', '45678', 'Scotiabank branch #45678 - Timmins, ON'),
#  ('003', '67890', 'RBC branch #67890 - Sudbury, ON'),
#  ('004', '12345', 'TD branch #12345 - London, ON'),
#  ('004', '34567', 'TD branch #34567 - Toronto, ON')]

... cout最终只显示0。它永远不会进入while循环。我究竟做错了什么?这只是我汇总来展示此问题的简单程序。我正在使用以下命令进行编译:{{1​​}},而我正在读取的文本文件称为test.txt,并且其中包含以下内容:

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main(){
       string input; 
       cout << "Input file name: "; 
       cin >> input; 
       string fName = "\"" + input + "\""; 

       ifstream myfile(fName.c_str()); 
       string content; 
       int count = 0; 

       while(getline(myfile, content)){
              count++;
       }

       cout << count << endl;
       myfile.close();

       return 0;
}

任何提示都非常感谢!

0 个答案:

没有答案