regExp in c: match a digit after <n> repetitions - c

i have a group of numbers that start with string, inside a string in C:
intr 250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 75963 0 0 0 0 0 0 0 0 0 0 0 0 0 6416543 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 29812 197 0 0 0 0 842664 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
i want to capture (match) only the 26th position of them (desired match: 1009565 on my return.)
i've tried this pattern:
(?:[0-9]+[[:space:]]){26}(?<![0-9])
but this is capturing whole string until the desired position.
How to achieve this with RegExp in C? someone could provide an sample source?
RegExp is the fastest (and lightest on system resources) way to do this? i need to repeat this operation various times in a second, all the uptime.
i'm confused on how to do this.

I believe using RegExp will make the problem more complicated, as suggested by others, using strtok is much easier.
You can parse the string at each space, and match the pattern you are trying to search for using strcmp from <string.h>.
Here is a basic idea of what you might want to use:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *string_search(char *string, unsigned position);
int main(void) {
char string[] = "250727985 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 238463729 0 0 0 0 0 8510 1009565 0 0 0 0 0";
char *check;
unsigned position = 26;
check = string_search(string, position);
if (check != NULL) {
printf("String %s found in position %d.\n", check, position);
} else {
printf("No string found in position %d.\n", position);
}
return 0;
}
char *string_search(char *string, unsigned position) {
char *number;
const char *delim = " ";
unsigned pos_count = 1;
number = strtok(string, delim);
while (number != NULL) {
if (pos_count == position) {
return number;
}
pos_count++;
number = strtok(NULL, delim);
}
return NULL;
}

The Unix strings library has strtok which divides a string into tokens given a delimiter. You should be able to simply iterate through the string until you get to the position you want.
A discussion on Stack Overflow of strtok is at Using strtok in c which has some sample code and gotchas.

Related

What is the difference between char array[100]; and char array[100] = "";?

I am trying to write a C program that loads a file, reads it and outputs the longest line from the file together with the number of symbols. The result is written in another file. The code seems to be working like it is, but I would like to understand why I get wrong results once I slightly change the definition of the arrays, to remove the equals to empty quotes (= "") from the maxLine definition. For example, if I write the following:
char currentLine[100];
char maxLine[100];
then I get unwanted results.
Here is the whole function:
#define MAX_FILE_NAME 50
void maxCharRow()
{
FILE *fptr;
errno_t err;
char fileNameRead[MAX_FILE_NAME] = "test.txt";
char fileNameWrite[MAX_FILE_NAME] = "results.txt";
char currentLine[100];
char maxLine[100] = "";
if ((err = fopen_s(&fptr, fileNameRead, "r")) != NULL) {
printf("Could not open the file: %s\n", fileNameRead);
exit(1);
}
while (fgets(currentLine, sizeof(currentLine), fptr) != NULL)
{
if (strlen(maxLine) < strlen(currentLine))
{
strcpy_s(maxLine, currentLine);
}
}
printf("\nLongest line in file has %i symbols\nand its content is:%s", strlen(maxLine), maxLine);
((err = fopen_s(&fptr, fileNameWrite, "w")) != NULL); {
fprintf(fptr, "%s", maxLine);
exit(1);
}
fclose(fptr);
}
In c "" is an empty string terminated by null character 0.
Since char currentLine[100] is a local variable it is by default not initialized to zero and is filled with indeterminate values.
But if you initialize zero-th element to 0 or "" then c will initialize whole array with zeros.
example:
#include <stdio.h>
int main()
{
char arr0[100];
char arr1[100] = "";
char arr2[100] = {0};
for(int i=0; i<100; ++i){
printf("%d ", arr0[i]);
}
printf("\n");
for(int i=0; i<100; ++i){
printf("%d ", arr1[i]);
}
printf("\n");
for(int i=0; i<100; ++i){
printf("%d ", arr2[i]);
}
printf("\n");
}
Output:
0 0 0 0 0 0 0 0 -1 -75 -16 0 0 0 0 0 -62 0 0 0 0 0 0 0 -89 -82 55 -96 -4 127 0 0 -90 -82 55 -96 -4 127 0 0 85 -105 -84 -34 -92 127 0 0 1 0 0 0 0 0 0 0 101 88 74 28 -111 85 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 32 88 74 28 -111 85 0 0 -48 85 74 28 -111 85 0 0 -80 -81 55 -96
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Edit:
Also note that even if you set first eg. 3 elements to non-zero values char arr0[100] = {1,2,3} the rest of the elements will still be initialized to zero.
Assigning "" to an array null-terminates the array so it becomes a string.
It's like doing this:
char array[100] = { '\0' };
Note that this isn't needed at global scope because variables at global scope are already zero-initialized.

Golang read byte array from .. to and display result

First of all: I know that this is super basic question and you'd expect to find enough material on the internet and there probably is. I feel pretty stupid right now for not understanding it, so no need to point that out to me - I know^^
From the google Directory API, the response you get when reading a custom Schema is JSON-enocded:
https://developers.google.com/admin-sdk/directory/v1/reference/schemas
I copy/pasted that response and wanted to read it.
func main() {
jsonExample := `
{
"kind": "admin#directory#schema",
"schemaId": "string",
"etag": "etag",
"schemaName": "string",
"displayName": "string",
"fields": [
{
"kind": "admin#directory#schema#fieldspec",
"fieldId": "string",
"etag": "etag",
"fieldType": "string",
"fieldName": "string",
"displayName": "string",
"multiValued": true,
"readAccessType": "string",
"indexed": true,
"numericIndexingSpec": {
"minValue": 2.0,
"maxValue": 3.0
}
}
]
}
`
var jsonDec schemaExample
jsonExampleBytes := []byte(jsonExample)
m := make(map[string]interface{})
err := json.Unmarshal([]byte(jsonExample), &m)
byteStorage := make([]byte,600)
byteReader := bytes.NewReader(byteStorage)
res, err := byteReader.ReadAt(jsonExampleBytes,50)
fmt.Printf("############Hier : %v Err: \n%v",res,err)
fmt.Printf("Storage: %v\n",byteStorage)
byteStorage := make([]byte,600)
byteReader := bytes.NewReader(byteStorage)
res, err := byteReader.ReadAt(jsonExampleBytes,50)
fmt.Printf("Result : %v Err: %v\n",res,err)
fmt.Printf("Storage: %v\n",byteStorage)
This returns
res : 526 Err: <nil>
Storage: [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
.My question is how to implement a ReadFromTo method, which allows me to read a specific range of bytes from a byte array? And since the storage is empty, I also lack to understand how read that array back at all with the reader functions, only way I know how to pull it off is this:
fmt.Printf("Und die bytes to String: %v",string([]byte(jsonExample)))
From the docs (emphasis mine):
ReaderAt is the interface that wraps the basic ReadAt method.
ReadAt reads len(p) bytes into p starting at offset off in the underlying input source.
type ReaderAt interface {
ReadAt(p []byte, off int64) (n int, err error)
}
The argument to ReadAt (and Read in general) is the destination. You've got jsonExampleBytes and byteStorage the wrong way around.
package main
import (
"bytes"
"fmt"
)
func main() {
jsonExampleBytes := []byte(`{...}`)
byteReader := bytes.NewReader(jsonExampleBytes)
byteStorage := make([]byte, 600)
n, err := byteReader.ReadAt(byteStorage, 3)
fmt.Println("Storage:", string(byteStorage[:n]), err) // Storage: .} EOF
}
To access a sub-slice of bytes, you can in the most basic case just use the index operator:
array := make([]byte, 100)
bytes5to9 = array[5:10]
note here that the second index is exclusive.
If you need an io.Reader from these bytes, you can use
r := bytes.NewReader(array[5:10])
You can do this again, creating a second read for the same or a different range of the array.
The utility functions in io and ioutil might be of interest to you as well. See for example ioutil.ReadAll, io.Copy, io.CopyBuffer, io.CopyN and io.ReadFull.

What is wrong with my sudoku generate code in c?

That's my homework, making a sudoku game. I have done my algorithm but it's entering infinite loop. I didn't understand why.
I am trying create a random number and control it for find true number. Checking all columns and rows for find same number as like as our random number if it is, it's changing test number and if test has changed trying find another number for true number. Simple sudoku logic.
#include <stdio.h>
#include <stdlib.h>
int main() {
srand(time(NULL));
int num, col, row, row2, col2, test = 0;
int sudo[9][9] = {{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0}};
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
do {
test = 0;
num = rand() % 9 + 1;
//control
for (col2 = 0; col2 <= 8; col2++) {
if (num == sudo[col2][row]) {
test++;
}
}
for (row2 = 0; row2 <= 8; row2++) {
if (num == sudo[col][row2]) {
test++;
}
}
} while (test > 0);
sudo[col][row] = num;
}
}
//print
for (row = 0; row <= 8; row++) {
for (col = 0; col <= 8; col++) {
printf(" %d ", sudo[col][row]);
if (col == 2 || col == 5) {
printf(" | ");
}
}
if (row == 2 || row == 5) {
printf("\n---------------------------------");
}
printf("\n");
}
}
Your algorithm is broken, and I can demonstrate why. If it were possible to fill in a sudoku puzzle this way, it would also be trivial to solve a sudoku puzzle this way, which it is not.
Essentially your code boils down to the following. I've added early exits on the inner for-loops to stop searching once we find the number already in the current row or column (and actually made sense of what 99.9% of the world thinks of concerning "rows" and "columns" in a NxN matrix):
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NSIZE 9
void print_matrix(int const ar[][NSIZE])
{
for (size_t i=0; i<NSIZE; ++i)
{
for (size_t j=0; j<NSIZE; ++j)
{
fputc('0' + ar[i][j], stdout);
fputc(' ', stdout);
}
fputc('\n', stdout);
}
}
int main()
{
srand((unsigned)time(NULL));
int sudo[NSIZE][NSIZE] = {{0}};
int row, col;
for(row=0;row<NSIZE;++row)
{
for(col=0;col<NSIZE;++col)
{
int row2 = 0, col2 = 0, num;
printf("Trying ");
do
{
num = rand()%9+1;
printf("%d ", num);
for(row2=0; row2<NSIZE && num!=sudo[row2][col]; ++row2);
for(col2=0; col2<NSIZE && num!=sudo[row][col2]; ++col2);
}
while (row2 < NSIZE || col2 < NSIZE);
fputc('\n', stdout);
sudo[row][col] = num;
printf("sudo[%d][%d] = %d\n", row, col, num);
print_matrix(sudo);
}
}
}
As the loops progress, we report what number we're trying, and what the matrix looks like upon placement of a keeper. For example, a test run of the above initially can look like this:
Trying 8
sudo[0][0] = 8
8 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 1
sudo[0][1] = 1
8 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 9
sudo[0][2] = 9
8 1 9 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 6
sudo[0][3] = 6
8 1 9 6 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 3
sudo[0][4] = 3
8 1 9 6 3 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 4
sudo[0][5] = 4
8 1 9 6 3 4 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 4 6 7
sudo[0][6] = 7
8 1 9 6 3 4 7 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Trying 1 3 1 3 4 1 3 8 4 9 3 8 1 4 7 9 3 8 8 8 4 9 6 5
sudo[0][7] = 5
8 1 9 6 3 4 7 5 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
and this continues for perhaps a while. But eventually, unless you get extraordinarily lucky, the following is bound to happen (and this one went pretty deep before the wheels fell off):
Trying 1 6 3 4
sudo[6][6] = 4
8 1 9 6 3 4 7 5 2
1 3 5 4 8 6 2 7 9
3 6 4 8 7 9 5 2 1
7 9 1 2 4 5 3 8 6
4 7 3 9 2 8 6 1 5
5 4 2 3 6 1 8 9 7
6 8 7 1 9 3 4 0 0
0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0
Note we're about to try and populate sudo[6][7]. To do that we must find a number that is not in the sudo[r][7] column already, nor the sudo[6][c] row. But looking at the numbers already in those positions.
sudo[r][7] : {5,7,2,8,1,9}
sudo[6][c] : {6,8,7,1,9,3,4}
Therefore we're looking for a number from 1..9 that is NOT in: {1,2,3,4,5,6,7,8,9}, which we're NEVER going to find.
The algorithm is broken. There is a reason backtracking is used for tasks like this.

How to read in a specific number with sscanf from lines which are similar?

I'm trying to read in a specific number from a file with the sscanf()function in the C standard library. My example data comes from /proc/stat on a system running a Linux Kernel. Here is how it looks:
cpu 90158 11772 50095 6885572 36975 0 207 0 0 0
cpu0 22942 2975 12847 1720241 9655 0 58 0 0 0
cpu1 23879 2979 12080 1717405 12483 0 45 0 0 0
cpu2 21510 3105 12864 1722238 7790 0 57 0 0 0
cpu3 21824 2712 12301 1725687 7044 0 45 0 0 0
.
.
.
intr 2108705 19 28724 0 0 0 0 0 0 1 90871 0 0 204911 0 0 0 143 0 0 0 0 35 0 0 0 0 2362 0 101810 25 388 0 404786 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
ctxt 11136028
btime 1423918994
processes 155184
procs_running 2
procs_blocked 1
softirq 2109698 8 644880 168 19330 95660 0 24557 551780 3897 769418
I try to print all lines after the if-statement except those lines containing cpu directly followed (without a whitespace) by a number. That is the first line cpu should be printed with all the others but e.g. not the second line cpu0. Furthermore, the number directly following cpu in these lines should be stored as an int into int cpu.
I really tried to get my head around this and I can at least get the correct lines to print by using character classes. My if-statement contains:
sscanf(line, "cpu%*1[^ ]%d", &cpu) != 1)
where %1[^ ] means read in the line until you encounter a single whitespace (the 1 is probably not needed) and then store the following number %d in int cpu. But the wrong values are stored for the cpuN-lines. Instead of storing 0, 1, 2, 3 the values 22942, 23879, 21510, 21824 are stored. Now this can be traced back to my usage of %1[^ ]. But I tried so many different things that I might be missing the obvious. How can I print all lines except the ones where cpu is followed (without a whitespace) by a number N and storing N in int cpu? (I'd like to avoid using regex.h if possible.) Here is my code so far:
#include <stdio.h>
#include <stdlib.h>
main(void)
{
FILE * fp;
char * line = NULL;
size_t len = 0;
ssize_t read;
fp = fopen("/proc/stat", "r");
if (fp == NULL)
exit(EXIT_FAILURE);
while ((read = getline(&line, &len, fp)) != -1) {
int cpu;
if (sscanf(line, "cpu%*1[^ ]%d", &cpu) != 1) {
printf("%s", line);
}
}
fclose(fp);
if (line)
free(line);
exit(EXIT_SUCCESS);
}
You do not want to suppress the assignment of the digit after cpu. However, you also don't want to skip blanks, so you need to use either %c or %[] since all other formats (other than %n, which definitely doesn't count in this contex) skip leading blanks. That, in turn, means you need to read a character string, not an integer. So, the code should be:
char cpu_str[8]; // Allow for big machines!
if (sscanf(line, "cpu%7[^ ]", cpu_str) != 1)
printf("%s", line);
else if (sscanf(cpu_str, "%d", &cpu) != 1)
…oops: may the scanset should be %7[0-9]…
else
…cpu contains the cpu number…
suggest:
1) read line into buffer
2) strncmp (buffer, "cpu ") if 0, ignore line // notice trailing space
3) strncmp (buffer, "cpu" ) if 0, saved = atoi( buffer[3] ) // no trailing space
4) ignore all other lines

Unexpected data/unexpected segfault in a dynamically allocated array (for two dimensional use)

I am trying to create a 2D matrix in C (basically a dynamically allocatable 2d array of any given size) in both the most efficient and clean way possible. I had implemented such a thing in a larger project I am working on, but was having issues, and was able to narrow it down to the following.
I decided to malloc a giant array (I called it data), and then make an array of pointers (i called it cell) to be able to address the data in the big array in such a way that would make sense in a two-dimensional context (as in matrix[x][y] instead of data[ugly pointer arithmetic each time].) I thought this would be a good idea because it only calls malloc once, and so it would be faster, also, the allocated memory is in one consecutive block, which I believe (not too knowledgeable here) is a really good thing on some systems because of overhead in keeping track of allocated memory blocks.
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
typedef struct {
unsigned int sizeX;
unsigned int sizeY;
int **cell;
int *data; /* FOR INTERNAL USE ONLY */
} matrix;
matrix * matrix_malloc(unsigned int, unsigned int);
void matrix_free(matrix *);
void matrix_zero(matrix *);
void matrix_print(matrix *);
int
main(int argc, char *argv[])
{
int y, x;
matrix *theMatrix = NULL;
if (argc != 3) {
fprintf(stderr, "usage: %s sizeX sizeY\n", argv[0]);
return 1;
}
x = atoi(argv[1]);
y = atoi(argv[2]);
if (x < 10 || y < 10) {
fprintf(stderr, "usage: sizeX and sizeY must be >= 10\n");
return 1;
}
if ((theMatrix = matrix_malloc(x, y)) == NULL)
return 1;
matrix_zero(theMatrix);
/* lots of modification of the contents of the matrix would happen here */
matrix_print(theMatrix);
matrix_free(theMatrix);
return 0;
}
matrix *
matrix_malloc(unsigned int sizeX, unsigned int sizeY)
{
int i;
matrix *mat;
if ((mat = malloc(sizeof(matrix))) == NULL) {
return NULL;
}
if ((mat->data = malloc(sizeX * sizeY * sizeof(int))) == NULL) {
free(mat);
mat = NULL;
return NULL;
}
if ((mat->cell = malloc(sizeX * sizeof(int *))) == NULL) {
free(mat->data);
free(mat);
mat = NULL;
return NULL;
}
mat->sizeX = sizeX;
mat->sizeY = sizeY;
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
return mat;
}
void
matrix_free(matrix *mat) {
free(mat->cell);
free(mat->data);
free(mat);
mat = NULL;
}
void
matrix_zero(matrix *mat)
{
memset(mat->data, 0, mat->sizeX * mat->sizeY * sizeof(int));
}
void
matrix_print(matrix *mat)
{
unsigned int x, y;
for (x = 0; x < mat->sizeX; x++) {
for (y = 0; y < mat->sizeY; y++)
printf("%d ", mat->cell[x][y]);
printf("\n");
}
}
When I run the above program as ./a.out 10 10 there is no problem, but when I specify 30 20 instead of 10 10, I run into some issues.
On MacOSX (10.6.7) I get:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 540024880 540024880 540024880 540024880 540024880 808465461 943207474 875896880 875704368 540031032
842216505 926168880 926425140 909719605 540031032 926234424 909325360 875896888 825438256 540160816 10 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and then it exits properly.
On OpenBSD (4.7) I get this far:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
and then it just segfaults
My initial thought was that it was just some issue when allocating big enough blocks of memory that they cross page boundaries, but when I use 50 50 as the size, it runs fine.
I've narrowed it down this far, and tried googleing (not quite sure what it is I should be searching for though :| ) and asked a few of my friends, but this has them all stumped.
I found C. Segmentation Fault when function modifies dynamically allocated 2d array int matrix with pointers in C - memory allocation confusion but they were not relevant (as far as I can tell).
If somebody could please point me in the right direction, perhaps point out the problem or point me to some relevant documentation, I would be very grateful.
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
One of these SizeX'es needs to be a sizeY.
for (i = 0; i < sizeX; i++) {
mat->cell[i] = mat->data + mat->sizeX * i;
}
Imagine if sizeX is 100 and sizeY is 2. Here, you're laying out sizeX rows, 100 of them, each sizeX integers, 100 of them. Ooops.
That mat->sizeX should be mat->sizeY. You have sizeX rows, each with sizeY elements in them. So you need to skip forward sizeY integers to get to the next row.

Resources