Sponsored Content
Homework and Emergencies Homework & Coursework Questions Lex: analyzing a C file and printing out identifiers and line numbers they're found on Post 302732399 by Corona688 on Saturday 17th of November 2012 12:33:16 PM
Old 11-17-2012
That's not going to work. You can't store two things in one pointer that way. I'd use a structure with three things, the name, an array of lines, and the number of lines stored, so you don't have to do string operations all the time to find the name and don't try to constantly resize your strings.

Code:
struct {
        char *name;
        int *lines;
        int len;
} id[1000];
int i=0;

void insertId(char* str, int nLine)
{
        int x;
        for(x=0; x<i; x++)
        {
                if(strcmp(str, id[x].name) == 0)
                {
                        id[x].lines=realloc(id[x].lines, sizeof(int)*(id[x].len+1));
                        id[x].lines[id[x].len]=nLine;
                        id[x].len++;
                        return;
                }
        }

        id[i].name=strdup(str);
        id[i].lines=realloc(id[i].lines, sizeof(int));
        id[i].lines[0]=nLine;
        id[i].len=1;
        i++;
}

id[i].lines is an array. Access the elements individually, first element at 0, last element at id[x].lines[id[x].len-1]
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Printing line numbers

Maybe this question is out there, but I searched and didnt see it. To print my files I use more filename | lpr -Pprinter I would like to print my scripts with line numbers. How do I do this? (2 Replies)
Discussion started by: MizzGail
2 Replies

2. Shell Programming and Scripting

Print Selection of Line between two Identifiers.

I have a following containing DATA in the following format: DATA....------ --------------- -------------- DATA.....------ -------------------- ------------------ DATA....------ --------------- -------------- I want to extract the selective DATA in between identifiers and ... (4 Replies)
Discussion started by: parshant_bvcoe
4 Replies

3. Shell Programming and Scripting

Assign Line Numbers to each line of the file

Hi! I'm trying to assign line numbers to each line of the file for example consider the following.. The contents of the input file are hello how are you? I'm fine. How about you? I'm trying to get the following output.. 1 hello how are you? 2 I'm fine. 3 How about you? ... (8 Replies)
Discussion started by: abk07
8 Replies

4. Shell Programming and Scripting

grep on string and printing line after until another string has been found

Hello Everyone, I just started scripting this week. I have no background in programming or scripting. I'm working on a script to grep for a variable in a log file Heres what the log file looks like. The x's are all random clutter xxxxxxxxxxxxxxxxxxxxx START: xxxxxxxxxxxx... (7 Replies)
Discussion started by: rxc23816
7 Replies

5. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

6. Shell Programming and Scripting

cut a line into different fields based on identifiers

cat fileanme.txt custom1=, custom2=, userPulseId=3005, accountPolicyId=1, custom3=, custom4=, homeLocationId=0, i need to make the fields appear in next line based on identifier (,) ie comma so output should read cat fileanme.txt custom1=, custom2=, userPulseId=3005, ... (8 Replies)
Discussion started by: vivek d r
8 Replies

7. Homework & Coursework Questions

[solved]Perl: Printing line numbers to matched strings and hashes.

Florida State University, Tallahassee, FL, USA, Dr. Whalley, COP4342 Unix Tools. This program takes much of my previous assignment but adds the functionality of printing the concatenated line numbers found within the input. Sample input from <> operator: Hello World This is hello a sample... (2 Replies)
Discussion started by: D2K
2 Replies

8. Shell Programming and Scripting

Printing unique numbers from each file

I have some files named file1, file2, fille3......etc. These files are in a folder f1. The content of files are shown below. I would like to count the unique pairs of third column in each file. some files have no data. It should be printed as zero. Your help would be appreciated. file1 ARG... (1 Reply)
Discussion started by: samra
1 Replies

9. Shell Programming and Scripting

Avoid printing entire line if string not found

so im searching the process table with: ps -ef | awk -F"./rello.java" '{ print substr($0, index($0,$2)) }' I only want it to print everything that's infront of the "./rello.java". That's because im basically getting the arguments that was passed to the rello.java script. this works. ... (2 Replies)
Discussion started by: SkySmart
2 Replies

10. Shell Programming and Scripting

Printing string from last field of the nth line of file to start (or end) of each line (awk I think)

My file (the output of an experiment) starts off looking like this, _____________________________________________________________ Subjects incorporated to date: 001 Data file started on machine PKSHS260-05CP ********************************************************************** Subject 1,... (9 Replies)
Discussion started by: samonl
9 Replies
MYSQLI_STMT_FETCH(3)							 1						      MYSQLI_STMT_FETCH(3)

mysqli_stmt::fetch - Fetch results from a prepared statement into the bound variables

       Object oriented style

SYNOPSIS
bool mysqli_stmt::fetch (void ) DESCRIPTION
Procedural style bool mysqli_stmt_fetch (mysqli_stmt $stmt) Fetch the result from a prepared statement into the variables bound by mysqli_stmt_bind_result(3). Note Note that all columns must be bound by the application before calling mysqli_stmt_fetch(3). Note Data are transferred unbuffered without calling mysqli_stmt_store_result(3) which can decrease performance (but reduces memory cost). PARAMETERS
o $ stmt -Procedural style only: A statement identifier returned by mysqli_stmt_init(3). RETURN VALUES
Return Values +------+---------------------------------------------------+ |Value | | | | | | | Description | | | | +------+---------------------------------------------------+ | | | |TRUE | | | | | | | Success. Data has been fetched | | | | | | | |FALSE | | | | | | | Error occurred | | | | | | | |NULL | | | | | | | No more rows/data exists or data truncation | | | occurred | | | | +------+---------------------------------------------------+ EXAMPLES
Example #1 Object oriented style <?php $mysqli = new mysqli("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); } $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5"; if ($stmt = $mysqli->prepare($query)) { /* execute statement */ $stmt->execute(); /* bind result variables */ $stmt->bind_result($name, $code); /* fetch values */ while ($stmt->fetch()) { printf ("%s (%s) ", $name, $code); } /* close statement */ $stmt->close(); } /* close connection */ $mysqli->close(); ?> Example #2 Procedural style <?php $link = mysqli_connect("localhost", "my_user", "my_password", "world"); /* check connection */ if (mysqli_connect_errno()) { printf("Connect failed: %s ", mysqli_connect_error()); exit(); } $query = "SELECT Name, CountryCode FROM City ORDER by ID DESC LIMIT 150,5"; if ($stmt = mysqli_prepare($link, $query)) { /* execute statement */ mysqli_stmt_execute($stmt); /* bind result variables */ mysqli_stmt_bind_result($stmt, $name, $code); /* fetch values */ while (mysqli_stmt_fetch($stmt)) { printf ("%s (%s) ", $name, $code); } /* close statement */ mysqli_stmt_close($stmt); } /* close connection */ mysqli_close($link); ?> The above examples will output: Rockford (USA) Tallahassee (USA) Salinas (USA) Santa Clarita (USA) Springfield (USA) SEE ALSO
mysqli_prepare(3), mysqli_stmt_errno(3), mysqli_stmt_error(3), mysqli_stmt_bind_result(3). PHP Documentation Group MYSQLI_STMT_FETCH(3)
All times are GMT -4. The time now is 01:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy