Reading in Large Integer txt file into 2D array - c

Attempting to create a program that reasons in a large Text File and filled them into Rows + Columns. Eventually I'll have to computer best path but having trouble just implementing an Array that can store the values.
#include <stdio.h>
#include <stdlib.h>
//max number of characters to read in a line. //MAXN=5 means 4 chars are read, then a \0 added in fgets. See reference for functionality
#define MAXN 100L
int main(void) //char** argv also ok {
int i=0, totalNums, totalNum,j=0;
size_t count;
int numbers[100][100];
char *line = malloc(100);
FILE* inFile ;
inFile = fopen("Downloads/readTopoData/topo983by450.txt", "r"); //open a file from user for reading
if( inFile == NULL) { // should print out a reasonable message of failure here
printf("no bueno \n");
exit(1);
}
while(getline(&line,&count, inFile)!=-1) {
for(;count>0; count--,j++)
sscanf(line, "%d", &numbers[i][j]);
i++;
}
totalNums = i;
totalNum = j;
for(i=0;i<totalNums; i++){
for(j=0;j<totalNum;j++){
printf("\n%d", numbers[i][j]);
}
}
fclose(inFile);
return 0;
}

count does not tell you how many numbers there are. Further: sscanf(line, "%d", &numbers[i][j]); will just scan the same number every time.
So this
for(;count>0; count--,j++)
sscanf(line, "%d", &numbers[i][j]);
should be something like:
j = 0;
int x = 0;
int t;
while(sscanf(line + x, "%d%n", &numbers[i][j], &t) == 1)
{
x += t;
++j;
}
where x together with %n helps you move to a new position in the string when a number has been scanned.
Here is a simplified version that scans for numbers in a string:
#include <stdio.h>
int main(void) {
char line[] = "10 20 30 40";
int numbers[4];
int j = 0;
int x = 0;
int t;
while(j < 4 && sscanf(line + x, "%d%n", &numbers[j], &t) == 1)
{
x += t;
++j;
}
for(t=0; t<j; ++t) printf("%d\n", numbers[t]);
return 0;
}
Output:
10
20
30
40

Related

How so separate a string and order each record by left number in C

I have a file.csv . it contains two numbers separated by a comma.I put every line , such as string, in a pointer of char char *arr. My aim is to sort in ascending order by left number (number before comma i.e. //this is the example of what I have to sort, the whole example is below:
9514902
1134289
7070279
ecc..)
I tried strtok() but it delete the number after comma. I need both of the numbers for each couple.
To order the numbers I used Insertion Sort, trasforming my strings (couple of numbers with comma for me is a string) in long integers in order to compare them. swap function doesn't work because it returns me numbers that I've never passed him.
How can I resolve it?
main.c
#define SIZE 10
#define LEN 20
void swap(char *xp, char *yp){
char *temp=xp;
*xp = *yp;
*yp = *temp;
}
int main(){
FILE *fd = NULL;
fd = fopen("file.csv", "r");
int pos=0;
char (*arr)[LEN] = NULL;
arr = calloc ( SIZE, sizeof *arr);
while ( pos < SIZE && fgets ( arr[pos], sizeof arr[pos], fd)) {
++pos;
}
int i, j;
char *ptr;
for (i = 1; i < SIZE; i++){
char *p = strtok(arr[i], ",");
long pivot= strtol(p,&ptr,10);
char * c = strtok(arr[i-1], ",");
long value= strtol(c,&ptr,10);
for (j = i - 1; (j >= 0) && (value>pivot); j--){
swap(arr[j],arr[j+1]);
j--;
c = strtok(arr[j], ",");
value= strtol(c,&ptr,10);
}
}
}
file.csv
9514902,846
1134289,572
7070279,994
30886,48552
750704,1169
1385812,729
471548,3595
8908491,196
4915590,362
375309,212
You can do it without strtok too, Maybe this is not you looking for but you can look as another way of doing what you want, Have Fun
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(){
FILE* pFile = fopen("file.csv", "r");
if(pFile == NULL)
return 1;
char aBuf[256];
char aResult[1024];
aResult[0] = '\0';
// get each line
for(int i=0; fgets(aBuf, sizeof(aBuf), pFile) != NULL; i++){
// find comma in line
for(int j=0; j < strlen(aBuf); j++){
if(aBuf[j] != ',')
continue;
// copy everything before comma
char aAnotherBuffer[50];
strncpy(aAnotherBuffer, aBuf, j);
// convert it to integer
int FirstNum = atoi(aAnotherBuffer);
// get after of comma and convert it too
int SecondNum = atoi(aBuf + j + 1);
// make line that with sorted values
char aRes[256];
sprintf(aRes,
"%d,%d\n",
FirstNum < SecondNum? FirstNum: SecondNum,
FirstNum > SecondNum? FirstNum: SecondNum
);
// concatenate to result buffer
strcat(aResult, aRes);
// go for next line
break;
}
}
fclose(pFile);
// save results
{
FILE* pResFile = fopen("result.csv", "w");
if(pResFile){
fputs(aResult, pResFile);
fclose(pResFile);
}
}
return 0;
}

Count and get integers from a string using C

I am self teaching C programming.
I am trying to count number of int present in given string which are separated by space.
exp:
input str = "1 2 11 84384 0 212"
output should be: 1, 2, 11, 84384, 0, 212
total int = 6
When I try. It gives me all the digits as output which make sense since I am not using a right approach here.
I know in python I can use str.split (" ") function which can do my job very quickly.
But I want to try something similar in C. Trying to create my own split method.
#include <stdio.h>
#include <string.h>
void count_get_ints(const char *data) {
int buf[10000];
int cnt = 0, j=0;
for (int i=0; i<strlen(data); i++) {
if (isspace(data[i] == false)
buf[j] = data[i]-'0';
j++;
}
printf("%d", j);
}
// when I check the buffer it includes all the digits of the numbers.
// i.e for my example.
// buf = {1,2,1,1,8,4,3,8,4,0,2,1,2}
// I want buf to be following
// buf = {1,2,11,84384,0,212}
I know this is not a right approach to solve this problem. One way to keep track of prev and dynamically create a memory using number of non space digits encountered.
But I am not sure if that approach helps.
You want to build your number incrementally until you hit a space, then put that into the array. You can do this by multiplying by 10 then adding the next digit each time.
void count_get_ints(const char *data) {
int buf[10000];
int j = 0;
int current_number = 0;
// Move this outside the loop to eliminate recalculating the length each time
int total_length = strlen(data);
for (int i=0; i <= total_length; i++) {
// Go up to 1 character past the length so you
// capture the last number as well
if (i == total_length || isspace(data[i])) {
// Save the number, and reset it
buf[j++] = current_number;
current_number = 0;
}
else {
current_number *= 10;
current_number += data[i] - '0';
}
}
}
I think strtok will provide a cleaner solution, unless you really want to iterate over every char in the string. It has been a while since I did C, so please excuse any errors in the code below, hopefully it will give you the right idea.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[19] = "1 2 11 84384 0 212";
const char s[2] = " ";
char *token;
int total;
total = 0;
token = strtok(str, s);
while (token != NULL) {
printf("%s\n", token);
total += atoi(token);
token = strtok(NULL, s);
}
printf("%d\n", total);
return 0;
}
You can check the ascii value of each character by doing c-'0'. If it's between [0,9], then it's an integer. By having a state variable, when you're inside an integer by checking if a given character is a number of space, you can keep track of the count by ignoring white space. Plus you don't need a buffer, what happens if data is larger than 10,000, and you write pass the end of the buffer?, undefined behavior will happen. This solution doesn't require a buffer.
Edit, the solution now prints the integers that are in the string
void count_get_ints(const char *data) {
int count = 0;
int state = 0;
int start = 0;
int end = 0;
for(int i = 0; i<strlen(data); i++){
int ascii = data[i]-'0';
if(ascii >= 0 && ascii <= 9){
if(state == 0){
start = i;
}
state = 1;
}else{
//Detected a whitespace
if(state == 1){
count++;
state = 0;
end = i;
//Print the integer from the start to end spot in data
for(int j = start; j<end; j++){
printf("%c",data[j]);
}
printf(" ");
}
}
}
//Check end
if(state == 1){
count++;
for(int j = start; j<strlen(data); j++){
printf("%c",data[j]);
}
printf(" ");
}
printf("Number of integers %d\n",count);
}
I believe the standard way of doing this would be using sscanf using the %n format specifier to keep track of how much of the string is read.
You can start with a large array to read into -
int array[100];
Then you can keep reading integers from the string till you can't read anymore or you are done reading 100.
int total = 0;
int cont = 0;
int ret = 1;
while(ret == 1 && total < 100) {
ret = sscanf(input, "%d%n", &array[total++], &cont);
input += cont;
}
total--;
printf("Total read = %d\n", total);
and array contains all the numbers read.
Here is the DEMO
Example using strtol
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <errno.h>
#include <ctype.h>
int count_get_ints(int output[], int output_size, const char *input) {
const char *p = input;
int cnt;
for(cnt = 0; cnt < output_size && *p; ++cnt){
char *endp;
long n;
errno = 0;
n = strtol(p, &endp, 10);
if(errno == 0 && (isspace((unsigned char)*endp) || !*endp) && INT_MIN <= n && n <= INT_MAX){
output[cnt] = n;
while(isspace((unsigned char)*endp))
++endp;//skip spaces
p = endp;//next parse point
} else {
fprintf(stderr, "invalid input '%s' in %s\n", p, __func__);
break;
}
}
return cnt;
}
int main(void) {
const char *input = "1 2 11 84384 0 212";
int data[10000];
int n = sizeof(data)/sizeof(*data);//number of elements of data
n = count_get_ints(data, n, input);
for(int i = 0; i < n; ++i){
if(i)
printf(", ");
printf("%d", data[i]);
}
puts("");
}
Assuming you don't have any non-numbers in your string, you can just count the number of spaces + 1 to find the number of integers in the string like so in this pseudo code:
for(i = 0; i < length of string; i++) {
if (string x[i] == " ") {
Add y to the list of strings
string y = "";
counter++;
}
string y += string x[i]
}
numberOfIntegers = counter + 1;
Also, this reads the data between the white spaces. Keep in mind this is pseudo code, so the syntax is different.

allocate and fill 2d-array in a function using mpi in C

Im tryin to read a ascii-file consisting of 2 columns of and a variable nr of rows. Reading is done by processor 0 and then the data is distributed using MPI_Bcast(...). I had to create a 1-dim buffer array which contains the data and is sent to all other procs because I haven't found a way to directly broadcast the 2-dim. data.
There's something wrong with the allocation of the array arr. When I try to print the data, the first 3 rows are printed and then I get a segmentation fault. Most likely there are several mistakes.
I really tried to make the example as simple as possible.
my code:
//global variables
...
double **testarray;
int dim;
...
int main(int argc, char **argv){
...
...
read_file("test.txt",&testarray,&dim);
}
int read_file(char* infilename,double ***arr,int *rowsout)
{
FILE* infile;
int i,j,ch,number_of_lines=0;
int rows=0;
//first count lines
if(myid==0) //myid is processor id
{
infile = fopen(infilename, "r");
do
{
ch = fgetc(infile);
if(ch == '\n')
number_of_lines++;
} while (ch != EOF);
if(ch != '\n' && number_of_lines != 0)
number_of_lines++;
//close file
fclose(infile);
rows=number_of_lines-1;
*rowsout=rows;
}
// every proc should know about length of file in order
//to be able to allocate memory
MPI_Bcast(rowsout,1,MPI_INT,0,MPI_COMM_WORLD);
//allocate memory
double *buf; //1D-buffer for 2D-array
MPI_Alloc_mem((*rowsout)*2*sizeof(double), MPI_INFO_NULL, &buf);
MPI_Alloc_mem((*rowsout)*sizeof(double*), MPI_INFO_NULL,arr);
for (i = 0; i < (*rowsout); i++) {
MPI_Alloc_mem(2*sizeof(double),MPI_INFO_NULL,&arr[i]);
}
// Now read file on proc 0
if(myid==0)
{
infile=fopen(infilename,"r");
for(i=0;i<rows;i++)
{
for(j=0;j<2;j++)
{
fscanf(infile,"%lf",arr[i][j]);
printf("arr[%d][%d]:%e\n",i,j,(*arr)[i][j]);
}
}
fclose(infile);
}
return 0;
//dont go further, error occurs before loop finishs
MPI_Bcast(buf,(rows)*2,MPI_DOUBLE,0,MPI_COMM_WORLD);
//now reconstruct array from buffer
for(i=0;i<(*rowsout);i++)
{
for(j=0;j<2;j++)
{
*arr[i][j]=buf[i*2+j];
}
}
MPI_Free_mem(buf);
return 0;
}
You have some issues with your arr as you guessed. Here is a fixed version of your code (minus the MPI stuff):
#include <stdio.h>
#include <stdlib.h>
void read_file(char *filename, double ***arr, int *rowsout);
int main(void)
{
double **testarray;
int dim;
read_file("test.txt", &testarray, &dim);
return 0;
}
void read_file(char *filename, double ***arr, int *rowsout)
{
FILE *infile;
int i, j, ch;
infile = fopen(filename, "r");
do
{
ch = fgetc(infile);
if (ch == '\n')
++*rowsout;
} while (ch != EOF);
rewind(infile);
*arr = malloc(sizeof **arr * *rowsout);
for (i = 0; i < *rowsout; ++i)
(*arr)[i] = malloc(sizeof ***arr * 2);
for (i = 0; i < *rowsout; ++i)
{
for (j = 0; j < 2; ++j)
{
fscanf(infile, "%lf", &(*arr)[i][j]);
printf("(*arr)[%d][%d]: %e\n", i, j, (*arr)[i][j]);
}
}
fclose(infile);
for (i = 0; i < *rowsout; ++i)
free((*arr)[i]);
free(*arr);
}
Example input: test.txt
0.01 0.02
0.3 0.4
5.0 6.0
Example output:
(*arr)[0][0]: 1.000000e-02
(*arr)[0][1]: 2.000000e-02
(*arr)[1][0]: 3.000000e-01
(*arr)[1][1]: 4.000000e-01
(*arr)[2][0]: 5.000000e+00
(*arr)[2][1]: 6.000000e+00
I believe the mistake comes with your fscanf() line. fscanf() wants a double * when you are using "%lf", but you are instead passing arr[i][j] and there's two things wrong with that: since arr is actually a pointer to your 2-D array, you will need to deference it first ((*arr)), and second, since it needs the address of the double, you will need to use &: &(*arr)[i][j].

reading variable length string arrays as ints

So I'm trying to read input from file that is formatted like this:
0 3 4 1
1 2 4
2
3
4 2
The piece of code that I've written here:
for (int i = 0; i < 5; i++){
if (fgets(line, MAX_LEN, in) != NULL){
printf("\n%s\n", line);
int length = strlen(line);
printf("line = %d\n", length);
for (int j = 2; j < length; j+=2){
char a;
a = line[j];
int u = a - '0';
printf("line[%d] = %d\n", j, u);
}
}
}
is very crude but handles the above single digit type input, however I need it to handle double digit values. I thought about using sscanf() but the line inputs can contain an arbitrary number of values so I don't know how many times to call it. Any assistance here would be great as I'm at a loss... Thank you!
I suggest you use strtol
Here is an example of how to use it :
#include <stdio.h>
#include <string.h>
int main() {
char str[20]="1 8 0 8 99"; // or your line
char *pt;
int numbers[10]; // array to read the numbers
int index = 0, i;
numbers[index] = strtol(str, &pt, 10); // decimal numbers
index++;
while(*pt !='\0' )
{
numbers[index] = strtol(pt, &pt, 10);
index++;
}
for(i=0; i<index; i++) printf("%d ", numbers[i]);
return 0;
}
The output in this example will be :
1 8 0 8 99
Now I let you the task of integrating strtol in your actual program good luck :)
for (int i = 0; i < 5; i++){
if (fgets(line, MAX_LEN, in) != NULL){
printf("\n%s\n", line);
int length = strlen(line);
printf("line = %d\n", length);
int len, u;
sscanf(line, "%*d%n", &len);
for (int j=len;sscanf(line + j, "%d%n", &u, &len)==1;j+=len){
printf("line[%d] = %d\n", j+1, u);//+1 : one space
}
}
}

I read a string from a file and store it into an array, but how do I know the length of the array?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1=0, len2=0;
int BUFSIZE = 1000;
char* string1[20];
char* string2[20];
FILE *fp1 = fopen("input1.txt", "r");
FILE *fp2 = fopen("input2.txt", "r");
if ((fp1 == 0)||(fp2 == 0)){
fprintf(stderr, "Error while opening");
return 0;
}
string1[i] = (char*)malloc(BUFSIZE);
string2[i] = (char*)malloc(BUFSIZE);
while (fgets(string1[i], BUFSIZE, fp1)) {
i++;
len1+=strlen(string[i]);
string1[i] = (char*)malloc(BUFSIZE);
len1+=strlen(string1[i]);
}
i = 0;
while (fgets(string2[i], BUFSIZE, fp2)) {
i++;
string2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand()%i;
int k = (j+1)%i;
fflush(stdout);
printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
printf("\n");
printf("%d", len1);
int x;
for(x = 0; x<i; x++){
free(string1[x]);
free(string2[x]);
}
scanf("%d", x);
fclose(fp1);
fclose(fp2);
return 0;
}
Thanks to user1807597's help, I finally realize reading two strings and storing into arrays. But I still have trouble in getting the length of the array, I try to put len+=strlen(string[i]); in the while loop, but the compiler breaks when debugging. Someone knows why? Thank you!
You have two main choices for the length of the array.
You make it big enough that you don't think you'll ever use more entries.
enum { MAX_LINES = 16 * 1024 };
char *lines[MAX_LINES];
size_t num_lines = 0;
You do dynamic allocation of the array.
char **lines = 0;
size_t max_lines = 0;
size_t num_lines = 0;
...
if (num_lines >= max_lines)
{
size_t new_lines = max_lines * 2 + 2;
char **space = realloc(lines, new_lines * sizeof(*space));
if (space == 0)
...deal with out of memory...
lines = space;
max_lines = new_lines;
}
Both systems work. Dynamic allocation is a little more fiddly the first few times you do it, but you don't run into problems until you run out of memory, and it takes a long time to run out of memory these days. Fixed allocation may run out of space if you guess wrong, and nominally wastes memory if you only deal with small files. The 'wasted' memory is usually very small these days.
Which is better depends on the scale of the problems you expect your program to deal with. If they'll be small, a fixed but generous allocation is sensible and easier. If they'll be large, the dynamic allocation is more sensible.
I have modified the code.I think now it's ok.You have wrong counting in the first while loop.And i++ should come after len1+=strlen(string[i]);.In the last scanf statement scanf("%d", x);,you have missed the operator '&'.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int main()
{
int i = 0, len1 = 0, len2 = 0;
int BUFSIZE = 1000;
/*
you have too few lines here,
If my file contains more than 20 lines,tragedy will occur!
*/
char* string1[20];
char* string2[20];
FILE* fp1 = fopen("input1.txt", "r");
FILE* fp2 = fopen("input2.txt", "r");
if ((fp1 == 0) || (fp2 == 0))
{
fprintf(stderr, "Error while opening");
return 0;
}
string1[i] = (char*)malloc(BUFSIZE);
string2[i] = (char*)malloc(BUFSIZE);
while (fgets(string1[i], BUFSIZE, fp1)!=NULL)
{
/*
i++;
*/
len1 += strlen(string1[i]);
i++;
string1[i] = (char*)malloc(BUFSIZE);
}
i = 0;
while (fgets(string2[i], BUFSIZE, fp2))
{
i++;
string2[i] = (char*)malloc(BUFSIZE);
}
printf("Output: \n");
srand(time(NULL));
int j = rand() % i;
int k = (j + 1) % i;
fflush(stdout);
printf("%d - %s %d -%s", j, string1[j], k, string1[k]);
printf("%d - %s %d -%s", j, string2[j], k, string2[k]);
printf("\n");
printf("%d", len1);
int x;
for (x = 0; x < i; x++)
{
free(string1[x]);
free(string2[x]);
}
/*
scanf("%d", x);
*/
scanf("%d",&x);
fclose(fp1);
fclose(fp2);
return 0;
}

Resources