Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Important: If you're upgrading snforge to version 0.48.0 or later, please read the 0.48.0 Migration Guide.

read_txt

Function for reading plain text files.

fn read_txt(file: @File) -> Array<felt252>;

â„šī¸ Info

Specific rules must be followed for snforge to correctly parse plain text files.

Read file format rules for more.

Example

File content:

'Hello Starknet!'
'Let's code in Cairo!'
"Example byte array"

Test code:

use snforge_std::fs::{FileTrait, read_txt};

#[test]
fn read_txt_example() {
    // Create an instance of `File` to be used later
    let file = FileTrait::new("data/hello_starknet.txt");

    // Read the content of the file
    let content = read_txt(@file);

    let expected = array![
        'Hello Starknet!',
        'Let\'s code in Cairo!', // Below is serialized byte array "Example byte array"
        0,
        6051711116678136165665715375637410673222009, 18,
    ];
    let mut i = 0;

    // Iterate through the content and compare with expected values
    while i != content.len() {
        println!("0x{:x}", *content[i]);
        assert(*content[i] == *expected[i], 'unexpected content');
        i += 1;
    };
}

Let's run the test:

$ snforge test read_txt_example
Output:
Collected 1 test(s) from snforge_library_reference package
Running 1 test(s) from tests/
0x48656c6c6f20537461726b6e657421
0x4c6574277320636f646520696e20436169726f21
0x0
0x4578616d706c652062797465206172726179
0x12
[PASS] snforge_library_reference_integrationtest::test_fs_read_txt::read_txt_example ([..])
Running 0 test(s) from src/
Tests: 1 passed, 0 failed, 0 ignored, [..] filtered out