How to get line number of the IASTNode using cdt APIs - eclipse-cdt

I am using CDT parser to read .c/.h/.cpp files. To get the line number of a variable/macro/function, i am using getStartingLineNumber(IASTNoder iastNode) of ASTNodes class. But the line number which is returned from the method is not correct. I think the method is not considering the empty lines in the input files(.c/.h/.cpp).
Can some one help me in getting correct line numbers of the code.
Example:
iastNode is instance of IASTFunctionDefinition
int lineNumber = ASTNodes.getStartingLineNumber(iastNode);
Thanks you so much in advance.

Related

(C++) Using curses to print new line read in from file

The following is in a text file
To the north, is the entrance to Room 2.\nThere are six suspects in this room:\n\tAdam\n\tSofia\n\tLucas\n\tDaniel\n\tChris\n\tJack\n\tTiana.
This line is being read in and being stored.
I am trying to use printw() to output this with the lines and tabs, however it just prints out as is with the '\n' and the '\t'. What are some possible solutions to this?
Try using the printf() function. By checking the documentation they are the same functions, couldn't find the differences.

c++ delete specific line number in a text file

I've been having some trouble achieving this functionality. I haven't been able to find code resolving this specific issue anywhere.
Thank you for taking time to help me, it means a lot
I made my own getline() with while() in it with an integer that increases with every line, and the number that i entered ignores the number of that line, writes to a new file, removes the original file, and renames the temporary file to the original, many thanks

Checking an external file against set of functions and printing error alongwith line number using c++

I have a GEDCOM file, which I am supplying to my program, my program checks the content of GEDCOM file against a set of functions.
If the contents of file, fail to match the requirements of function, it should throw an error along with the line number on the GEDCOM file where error exists.
I would also like to mention that I have tried using macros like LINE, FILE but they are printing the contents of source code like line of source code and file name of source code.
Thanks in advance
If you are reading the file one line at a time you can keep a counter of the number of lines you have read from the file.
If you already have the whole file in one big buffer you can scan the number of newline or return characters.
If you provide a MCV example demonstrating your code reading in the file I can maybe help more.

Python: Iterate through an html file

I'm trying to iterate through an html file from the internet.
target = br.response().read()
for row in target:
if "[some text]" in row:
print next(target)
The problem is this loop iterates over each character in the html file, so it'll never find a match. How do I get it to iterate through each row instead?
I've tried target = target.splitlines() , but that really messes up the file.
What you basically want to achieve is the following (reading from a file, as your header suggests):
#!/usr/bin/env python
import sys
with open("test.txt") as file:
for line in file:
if "got" in line:
print "found: {0}".format(line)
You want to open your file ("test.txt").
You read each line (for .. in)
and look if the line contains a string, where in comes in nice:)
If you are interested in the line number:
for index, line in enumerate(file):
But beware the index starts with 0, so the current line number is index+1
Analog, if you want to read from a String as a file, take a look at StringIO.
Take a look at the page source for the file you're viewing, because that's what you're getting back as a response. I have a feeling the response you're getting doesn't actually have new lines where you want it to. For pages like http://docs.python.org/ where the source is readable your splitline() method works great, but for sites where the source essentially has no line breaks, like Google's homepage, it's a lot closer to the problems you're experiencing.
Depending on what you are trying to achieve, your best bet might be to use an html/xml parsing library like lxml. Otherwise using re is probably a pretty safe approach. Both are a lot better than trying to guess where the line breaks should be.

How do I get specific lines from a text file in C++?

I need some help with C++
I am trying to create a program which contains excersises to practice the different German cases.
Hard-coding all questions and respective answers seems like an awful lot of work, and super inefficient.
What I want my program to do, is: grab a random line from file X, and grab the same line number from file Y. (This seems like the easiest way to get both questions and answers from external files.) To me, it seems the most logical to get a random number, and use that as a line number. But, that's about how far I got...
I know basic C++, but am very eager to learn.
Can anyone please explain to me how to pull this off, including all necessary command?
First, I would recommend that you store questions and answers in the same text file, probably by alternating between a question line and then an answer line. This will make correcting mistakes, adding/removing questions, and general maintenance of your data easier.
But if you want to keep them in separate files, the following code snippet will read your text file in and store the questions in an array (an stl vector) which you can then index or iterate any way you'd like:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
int main()
{
std::ifstream file("questions.txt");
std::string line;
std::vector<std::string> questions;
while (std::getline(file, line))
{
questions.push_back( line );
}
// Now do something interesting with your questions. You can index them
// like this: questions[5], or questions[random_index]
}
There are two ways of doing this:
If you are planning on getting question/answer pairs you would be best to just read the who file line by line and store all the lines. Then you just look it up in the array.
If for some reason you only want to get one line at a time you'll have to read lines and count until you've gotten to the line you want.
you may have a keyword for each line, like an id.
that id can be paired to both questions, and answers if you have multiple files. or just pair the question, with the answer same order, or even same file.
You are constructing a database.
You should use a database.
The problem is that the question and answers are variable length records, which make positioning difficult. If all the records were the same length, you could position to a random record much faster.
In order to find a text line, you will need to read past all the other newlines (since they are not in the same column in every line). This is fine if you only need to search once, but very slow to search many times. Now comes the reason for the database.
To make finding the questions and answers faster, create an index file or table. (Starting to smell like a database). The index file will contain records of the form [question #, file position] where file position is the position in the question's file that the question starts on.
You would load this file into memory and use it to index into the "questions" file. By storing the index contents into a file, you won't have to construct it from scratch each time your program starts; only when the question's file changes.