Jump to content

How to open and read TEXT file in systemC


Arjun_07

Recommended Posts

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

Link to comment
Share on other sites

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;'.

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...