How to set Control-L as variable

M

moonhkt

Hi All

I am testing how to checking control-L in text file.

abc.txt file
1 Line 1
2 ^L line 2 with control-L at 3
^L
some character before contro-l ^L
5
~

Output
openfile abc
Input file abc.txt
B 1 1 Line 1
B 2 2
line 2 with control-L at 3
A 3

Page ended with control-L
B 4 some character before contro-l

Page ended with control-L
B 5 5

How to replace ^L with varaible ? for line if (!strncmp(line,"^L",1))
{ /* 12 form feed */ ?

and int ff = (int) '^L'; how to using \f replace ^L ?

/* gcc -o openfile openfile.c
check control-L location
*/

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>

#define PAGELENGTH 512
#define LINELENGTH 512
#define NAMELENGTH 64

int idx;
int ftype;
char *page[PAGELENGTH];
char line[LINELENGTH];
char line2[LINELENGTH];
char *ifile;
char *ofile;
FILE *ifp;
FILE *ofp;
char key[32];

main(int argc, char *argv[])
{
int result;
int len ;
int ff = (int) '^L';
ifile = malloc (NAMELENGTH);
result = sprintf(ifile, "%s%s", argv[1], ".txt");
printf("Input file %s\n", ifile);
ifp = fopen (ifile, "r");
if (ifp == NULL) { printf ("Failed to open input file.
\n"); exit (1); }

while (fgets(line, LINELENGTH, ifp) != NULL) {
idx++;
if (!strncmp(line,"^L",1)) { /* 12 form feed */
printf("A %d %s",idx,line);
} else {
printf("B %d %s",idx,line);
}
len = strlen(line);
if (line[len-2] == ff ) {
printf ("Page ended with control-L\n");
}
}
}
 
E

Edwin van den Oetelaar

moonhkt said:
Hi All

I am testing how to checking control-L in text file.

instead of writing '^L' write '\f' or just 12
Good luck,
Edwin
 
C

Chïna Blüe Öyster Cult

Edwin van den Oetelaar said:
instead of writing '^L' write '\f' or just 12

And for a string "^L",
char FF[] = "\f";
or
char FF[] = "\x0C";
or
char FF[] = {12, 0};
 
M

moonhkt

instead of writing '^L' write '\f' or just 12

And for a string "^L",
    char FF[] = "\f";
or
    char FF[] = "\x0C";
or
    char FF[] = {12, 0};

--
Damn the living - It's a lovely life.           I'm whoever you want me to be.
Silver silverware - Where is the love?       At least I can stay in character.
Oval swimming pool - Where is the love?    Annoying Usenet one post at a time.
Damn the living - It's a lovely life.                              Blessed be.


#gcc -o openfile openfile.c
openfile.c: In function 'main':
openfile.c:40: warning: passing argument 2 of 'strncmp' makes pointer
from integer without a cast

Using Single Quote.
#openfile abc
Input file abc.txt
B 1 1 Line 1
B 2 2
line 2 with control-L at 3
B 3

Page ended with control-L
B 4 some character before contro-l

Page ended with control-L
B 5 5

Using Double quotes can not check Contro-l
openfile abc
Input file abc.txt
B 1 1 Line 1
B 2 2
line 2 with control-L at 3
A 3

B 4 some character before contro-l

B 5 5


what are different using quote and double quotes ? Double quotes not
work.
int ff1 = (int) '\x0C';
int ff1 = (int) "\x0C";

And how to fix "openfile.c:40: warning: passing argument 2 of
'strncmp' makes pointer from integer without a cast" ?
 
C

Chïna Blüe Öyster Cult

moonhkt said:
what are different using quote and double quotes ? Double quotes not
work.
int ff1 = (int) '\x0C';
int ff1 = (int) "\x0C";

And how to fix "openfile.c:40: warning: passing argument 2 of
'strncmp' makes pointer from integer without a cast" ?

Single quoted literals 'X' are an integer whose value is the encoding of the
quoted character.

Double quoted strings are a char[], an array of characters, where the element
values are the encodings of the characters of the string followed by a zero
character.

You should assign a double quoted string to a char* or char[]:
char *ff1 = "\x0C";
 
K

Keith Thompson

moonhkt said:
#gcc -o openfile openfile.c
openfile.c: In function 'main':
openfile.c:40: warning: passing argument 2 of 'strncmp' makes pointer
from integer without a cast

Using Single Quote.
#openfile abc
Input file abc.txt
B 1 1 Line 1
B 2 2
line 2 with control-L at 3
B 3

Page ended with control-L
B 4 some character before contro-l

Page ended with control-L
B 5 5

Using Double quotes can not check Contro-l
openfile abc
Input file abc.txt
B 1 1 Line 1
B 2 2
line 2 with control-L at 3
A 3

B 4 some character before contro-l

B 5 5


what are different using quote and double quotes ? Double quotes not
work.
int ff1 = (int) '\x0C';
int ff1 = (int) "\x0C";

And how to fix "openfile.c:40: warning: passing argument 2 of
'strncmp' makes pointer from integer without a cast" ?

You've shown us some error messages, but they're useless without the
code that generated them. Post your code in a form that lets us try to
compile it ourselves.

Single quotes are for characters constants; double quotes are for string
literals. Character constants are of type int, and are typically used
to initialize or assign to objects of some character type; string
literals are of type "array of char" (there are exceptions that needn't
concern us for now).
 
G

Guest

How to replace ^L with varaible ? for line  if (!strncmp(line,"^L",1))

Assuming you mean: How to replace "^L" ...

static char varaible[] = "^L";

if (!strncmp(line, varaible, 1))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top