Arjun_07 Posted June 26, 2020 Report Share Posted June 26, 2020 Hello Everyone, I am trying to open text file, file is opening but when i am trying to read data in C[MEM_DEPTH] buffer, nothing is copied from the file, when i am printing the C[MEM_DEPTH] only initialize value =0 is printing. The task is to read data from file into the buff_1[MEM_DEPTH], then this will be send to source file for writing into the memory. i am attaching my testbench and text file . please let me know where i am making a mistake. Thanks and Regards Arjun memory_testbench.cpp TEXT.txt Quote Link to comment Share on other sites More sharing options...
Eyck Posted June 26, 2020 Report Share Posted June 26, 2020 AFAICS you don't increment the index i in the while loop. But your code is way to complex.: std::ifstream ifs("TEXT.txt"); if(ifs.is_open()){ int buf = 0; for (int i = 0; i < MEM_DEPTH; i++) { ifs >> buf; buff_1[i]=buf } } ifs.close(); should replace everything from fopen() until fclose(). And you should avoid using macros, they will bite you. '#define MEM_DEPTH 20' should become 'const size_t MEM_DEPTH=20;'. David Black 1 Quote Link to comment Share on other sites More sharing options...
David Black Posted June 26, 2020 Report Share Posted June 26, 2020 You might want to add an else clause and issue an error message in case the file does not open successfully (e.g. due to the filename or location being wrong or permissions). I/O errors always bite when you least expect them and they often waste time figuring out what happened. Clear error messages are very helpful. For more information: <https://www.cplusplus.com/reference/fstream/ifstream/is_open/> look at the example. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.