Read C file in specific order and find substring [closed] - c

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Can someone give me introduction of how to read text file in this order:
Line of text here
number1(startposition)
number2(endposition)
Line of text here
number1(startposition)
number2(endposition)
Line of text here
number1(startposition)
number2(endposition)
And then find substring of the line with text using number1 as startposition and number2 as endposition and copy the substring in new string.

OK, here is an introduction:
Read one line using fgets in a buffer str
Read 2 lines
Convert those 2 lines to numbers using strtol
Check that the indices make sense (i.e. that they are in the string)
Allocate a new string with length = end - start + 1
Use memcpy to copy end - start bytes from str + start
Make sure to add a 0-terminator to your destination string
Go back to first step
At each point when you do a fgets call be prepared for it to fail.

Related

Find the number of characters from the first character of nth occurrence of a character in a text file without using an array and strchr function? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Fore example lets assume the following is a text in a file:
"I want to find number of characters from the first character is the nth occurrence of a character in a text file, but, I want to do this without declaring an array, storing the text file in it and applying the strchr function."
Lets say I want to find the position of the second new line character in the text? How many number of characters is the new line from the first character in the text? For e.g. The first occurrence character 't' is the 6th character of the text file.
If it is possible can someone please explain how? If no, can someone please explain why?
That can be done in an operating system that supports memory mapped files. This includes Windows and POSIX operating systems such as Linux.
In POSIX it is done using mmap(); there is example code in the documentation.
For Windows there is an API for for memory mapped files. Again, sample code is included in the documentation.

How to implement word spell checking with c? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I need to create spell checker in c for my assessment. I manage to start the work - I have text file which content all the dictionary, I code how to read the files, how to compare chosen file with the dictionary file and now I need to print out to new text file where the misspelled words been found, what they are, and their correct version. The big problem is, that I have no idea how to do this. My code right now can say that there is a difference between the files. But I don't know how make strcmp check string by string, word by word, if something is wrong.
In dictionary files are all the words, so of course, if my program is reading other file, compare, then writes all the words which aren't in the file to the new output file with errors, these output-error words will be also just random words, which aren't even in the text file, or connected with the text file.
I hope I explained my problem well and there is somebody who would tell me how to fix this problem. I don't even ask for the code, I just need some idea how I would need to code the rest of the program. And sorry, for my English, it's my second language, so I still make grammatical mistakes.
Here are some steps you can follow:
read the dictionary into a memory structure, for example an array of strings, which you will sort in lexicographical order (with strcmp).
read the file line by line, and for each line iterate these steps:
initialize a highlight line with spaces, same length as the line read.
skip characters that cannot be part of a word with strcspn(), save the index i.
scan the characters that can be part of a word with strspn() save this number n.
if n is 0, this is the end of line
look up the word at index i with n chars in the dictionary (potentially ignoring the case)
if the word cannot be found, set the corresponding characters in the warning line with ^ characters.
update the index i += n and iterate.
if at least one word was not found in the line, output the line and the warning line.
Study these standard functions:
strspn()
strcspn()
qsort()
bsearch()

Converting assembly code to machine code in C manually [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I've been given a practise homework to do from college which requires me to manually convert assembly code to machine code. I will display the images of what the before and after looks like. It basically requires my to remove the comments (after semi colon) and white spaces after the instruction. I found this to be puzzling since im a C noob and ive been thrown into assembly code. I am planning to do this in notepad++. The assembly language im using is meant to be very simplified since x86 would be ridiculous to start off on. Any help would be appreciated!
As stated in the comment, this has nothing to do with machine code, it's just trivial string manipulation (essentially, it's sed 's/[[:space:]]*\(;.*\)\?$//'):
Read a line
Walk the string one character at time; each time you find a non-whitespace character, write down its position in a variable.
When you find either the end of the string (character 0) or a semicolon (character ';'), truncate the string to the character following the last non-whitespace one you found (effectively, set it to zero).
Write down the updated string to the other file.
Repeat until the input file is finished.

Printing big-length string not showing all chars [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I want to concatenate lots of strings together to produce a unique string which will be long enough. Here is an example code:
char *s;
s= strconcat("A big-length string",NULL);
fprintf(stdout,"%s",s);`
When I try to print it, the string is less that is printed is smaller than the actual string.
Assume that the strconcat() function works well. Do I have to malloc() some space for s??
Also when I try strlen(s) it shows me the right number like 1078. just the printing is the matter.
EDIT
The problem solved when after all strings I try to inject \n like this
s= strconcat("A big-length string","\n",NULL);
Output using printf/fprintf is usually buffered.
If the output file is connected to a terminal, output is line buffered, so the buffer will be flushed wfter each \n.
If the output file is not connected to a terminal, output is fully buffered, so the buffer will be flushed when it is full. Typical buffer sizes is 1kb-8kb.
to ensure that the buffer is flushed call:
fflush(stdout);
You can flush all file buffers with:
fflush(NULL);

read file in a specific region line by line in file [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a text file where every row has the format
A B:C
I want to read it using C language, but all i want to get in every line is what comes after the ':'. In other words I want to get only the C value in every line of the file, but all I want to do is read the file line-by-line and get every row's values.
Any idea to do that?
open the file for reading
fp = fopen("file.txt" , "r");
Use fgets() to read the line
fgets (str, MAX_LENGTH, fp);
Use Strtok to break according to ":"
ptr = strtok(str,":");
Read file line by line by using fgets()
fgets(line,MAX_SIZE,file_stream);
And use strchr(), to get the position from where C starts
char *ptr=strchr(line,':'); //ptr points to the : location
//now if you Move ptr location next to ':' ptr points to C
printf("C=%s",ptr+1);
And with out using additional pointer, You can do like this
printf("C=%s",strchr(line,':')+1);
Repeat these steps until reaching end of file. By checking the return value of fgets() against NULL.
Note: In this A B:C , if A or B consists : then You need to Apply logic accordingly.
You can also use strrchr()

Resources