file-io

Counting number of lines in the file in C

余生长醉 提交于 2021-02-05 07:32:05
问题 I'm writing a function that reads the number of lines in the given line. Some text files may not end with a newline character. int line_count(const char *filename) { int ch = 0; int count = 0; FILE *fileHandle; if ((fileHandle = fopen(filename, "r")) == NULL) { return -1; } do { ch = fgetc(fileHandle); if ( ch == '\n') count++; } while (ch != EOF); fclose(fileHandle); return count; } Now the function doesn't count the number of lines correctly, but I can't figure out where the problem is. I

Cannot open file using fopen (php)

我的未来我决定 提交于 2021-02-05 06:47:06
问题 I am trying to open a file for reading in php script but having trouble. This is my code $fileHandle = fopen("1234_main.csv", "r")or die("Unable to open"); if (!file_exists($fileHandle)) { echo "Cannot find file."; } The script is in the same directory as the file I am trying to read and there are no other read/write permission errors as I can create/read other files in the same directory. When I run the script I just get the "Cannot find file" error message. Why is this error message being

Initializing a vector from a text file

Deadly 提交于 2021-02-04 19:44:10
问题 I am attempting to write a program which can read in a text file, and store each word in it as an entry in a string type vector. I am sure that I am doing this very wrong, but it has been so long since I have tried to do this that I have forgotten how it is done. Any help is greatly appreciated. Thanks in advance. Code: #include <iostream> #include <fstream> #include <vector> #include <string> using namespace std; int main() { vector<string> input; ifstream readFile; vector<string>::iterator

Read text file line by line but only specific columns

心不动则不痛 提交于 2021-02-04 17:55:07
问题 How do we read a specific file line by line while skipping some columns in it? For example, I have a text file which has data, sorted out in 5 columns, but I need to read only two columns out of it, they can be first two or any other random combination (I mean, need a solution which would work with any combination of columns like first and third only). Code something like this open(1, file=data_file) read (1,*) ! to skip first line, with metadata lmax = 0 do while (.true.) ! read column 1 and

How to append to a file in java using formatter?

别说谁变了你拦得住时间么 提交于 2021-02-04 08:39:04
问题 i have a simple problem. i use this code to add some records in text file using java but every time i run this program, the file is being created again. i wanna to append records without creating file again ? Formatter x = null; try{ x = new Formatter("C:\\Users\\Hamada\\Downloads\\products.txt"); } catch(Exception e) { //System.out.println("NO Database"); } x.format("%d,%s,%s,%s,%d,%d,%d,%d,%d,%d,%d",id, name, type, brand, quantity, day1, month1, year1, day2, month2, year2); x.close(); 回答1:

Array declaration: C Segmentation fault

こ雲淡風輕ζ 提交于 2021-02-04 08:32:25
问题 I am trying to write an array (2x20000) on C. The test code is: #include <stdio.h> #include <stdlib.h> #include <math.h> double test( int smod ) { // // test subroutine // double vect_fma[2][20000]; int i; // write on file // FILE *f = fopen("file.txt", "w"); /////////////////// for( i = 1; i < 20001; i = i + 1 ){ // allocate the vector for the fma analysis vect_fma[1][i] = i*smod; vect_fma[2][i] = i*smod; if ( i%smod == 0 ) fprintf(f, "%f %f %f \n", 1.0*i, vect_fma[1][i],vect_fma[2][i] ); }

Will TextIOWrapper.tell reliably return zero at the beginning of a file?

会有一股神秘感。 提交于 2021-01-29 19:13:27
问题 The docs for TextIOBase.tell, from which TextIOWrapper inherits, state Return the current stream position as an opaque number. The number does not usually represent a number of bytes in the underlying binary storage. I am wondering, whether it will always return zero in the specific case that the file pointer is at the beginning of the file? I wrote a small, non-exhaustive, script to check some obvious scenarios, which returned zero for each case: import io import os rmodes = ['a', 'a+', 'r',

Is it possible to use String.Split() when NewLine is the delimiter?

好久不见. 提交于 2021-01-29 15:24:14
问题 I have a question which asks me to calculate something from an input file. The problem is, the lines in the file don't use any special character as delimiter, like , or | . I will show it down below. Data Communication 20 Visual Basic 40 The output I need to write to another file should look like this: Data communication 20 Visual Basic 40 Total Books : 60 The problem is, how can I specify the delimiter? Like when there is a symbol as in strArray = strLine.Split(",") . Since there is nothing

Parsing file in C++ and writing it into HTML in table

淺唱寂寞╮ 提交于 2021-01-29 07:59:17
问题 I need to convert existing log file into HTML report. I'm working on Windows platform. Below is the example of the log messages from the log file. Example of log file MyProcess1 16-06-14 8:32:50 8667 [Info] This is an information message MyProcess2 16-06-14 8:32:57 9876 [Warn] This warning message Please Note the format of the log file MyProcess1 "space" 16-06-14 "space" 8:32:50 "space" 8667 "space" [Info] "space" This "space" is "space" an "space" information "space" message Expected HTML

How to read files outside main.go, and make it work on AWS lambda

我的未来我决定 提交于 2021-01-29 07:15:10
问题 On AWS Lambda, I want to use CSV file outside main.go Of course this works when I follow these steps. cd ~/go/src/lambda-go-sample GOOS=linux go build main.go zip main.zip main upload to Lambda and test => "Hello ƛ!" main.go package main import ( "github.com/aws/aws-lambda-go/lambda" ) func hello() (string, error) { return "Hello ƛ!", nil } func main() { lambda.Start(hello) } Now I add sample.csv on the same directory of main.go ,and add code to read it. lambda-go-sample |- main.go |- sample