Print array to a file
Either var_export
or set print_r
to return the output instead of printing it.
Example from PHP manual
$b = array (
'm' => 'monkey',
'foo' => 'bar',
'x' => array ('x', 'y', 'z'));
$results = print_r($b, true); // $results now contains output from print_r
You can then save $results
with file_put_contents
. Or return it directly when writing to file:
file_put_contents('filename.txt', print_r($b, true));
How save a array to text file in python?
This is a possible solution:
data = [['nameservers','panel'], ['nameservers','panel']]
with open("output.txt", "w") as txt_file:
for line in data:
txt_file.write(" ".join(line) + "\n") # works with any number of elements in a line
How to write an array to file in C
Since the size of the data is fixed, one simple way of writing this entire array into a file is using the binary writing mode:
FILE *f = fopen("client.data", "wb");
fwrite(clientdata, sizeof(char), sizeof(clientdata), f);
fclose(f);
This writes out the whole 2D array at once, writing over the content of the file that has been there previously.
how to write an array to a file Java
Like others said, you can just loop over the array and print out the elements one by one. To make the output show up as numbers instead of "letters and symbols" you were seeing, you need to convert each element to a string. So your code becomes something like this:
public static void write (String filename, int[]x) throws IOException{
BufferedWriter outputWriter = null;
outputWriter = new BufferedWriter(new FileWriter(filename));
for (int i = 0; i < x.length; i++) {
// Maybe:
outputWriter.write(x[i]+"");
// Or:
outputWriter.write(Integer.toString(x[i]);
outputWriter.newLine();
}
outputWriter.flush();
outputWriter.close();
}
If you just want to print out the array like [1, 2, 3, ....]
, you can replace the loop with this one liner:
outputWriter.write(Arrays.toString(x));
printing an array from .txt file java
import java.io.*;
import java.util.ArrayList;
import java.util.*;
public class ArrayOperations {
public static void main(String[] args) throws Exception{
List<Integer> readFromFile = readFromFile("data.txt") ;
printArray(readFromFile) ;
}
public static List<Integer> readFromFile(String fileName)
throws FileNotFoundException
{
File f = new File(fileName);
Scanner fileIn = new Scanner (f);
List<Integer> list = new ArrayList<Integer>();
while (fileIn.hasNextInt()){ // quit when you encounter ‘Q’
int num = fileIn.nextInt();
list.add(num);
}// end while
fileIn.close();
return list;
} // end readFromFile
public static String printArray( List<Integer> readFromFile){
String str = "";
for(int i = 0; i < readFromFile.size(); i++){
str = str + Integer.toString(readFromFile.get(i)) + " ";
}
System.out.println(str);
return str;
}
}
print array contents to a file using gdb
You can use:
(gdb) set logging file large_array.txt
(gdb) set logging on
By default the logging file name is gdb.txt
You can find more details at: https://sourceware.org/gdb/onlinedocs/gdb/Logging-Output.html
There is also one WA gdb --args a.out arg1 ... |& tee gdb_out.txt
Related Topics
How to Use Open Id as Login System
How to Increment Letters Like Numbers in PHP
Laravel App Stopped Working After Upgrading to PHP 8
Convert All Types of Smart Quotes with PHP
Initialize Class Property with an Anonymous Function
PHP Pdo Prepare Repetitive Variables
How to Use Pdo's Prepared Statement for Order by and Limit Clauses
PHP Get Url of Redirect from Source Url
How to Logout from a Session in PHP
Laravel: How to Change the Default Auth Password Field Name
PHP Create a Multidimensional Array from an Array with Relational Data
Remove Bom () from Imported .CSV File
Magento - Retrieve Products with a Specific Attribute Value
Reconstruct/Get Source Code of a PHP Function
How to Get Rid of MySQL Error 'Prepared Statement Needs to Be Re-Prepared'